注册 登录
编程论坛 VB6论坛

VB求助:扫描文件将List1里文件名为收索对象路径显示在List2里

qq251357 发布于 2012-03-23 15:18, 885 次点击
'以下是收索代码
'问题是为:不知道怎么将List1的全部(全部哦!可以声明:i 依次扫描..)收索文件名为收索对象
'收索到了就会将文件的路径显示在List2的里面!
'我是新手拜托各位老大帮帮忙!

工程事例图片:
只有本站会员才能查看附件,请 登录


Private Function SearchPath(ByVal sPath As String, ByVal FindName As String) As Boolean
   
    Dim sChildDir() As String
    Dim sFileName   As String
    Dim lDirNum     As Long
    Dim lCount      As Long
    On Error GoTo SearchPathErr
   
    If Right(sPath, 1) <> "\" Then sPath = sPath + "\"
    sFileName = Dir(sPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
    While sFileName <> ""
    Me.Caption = sPath '这里在标题栏显示当前查找的目录、你可以根据需要去掉
    '因为文件过多可能会给人死掉的假象、所以标题栏显示出当前查找的目录
        DoEvents

        If (GetAttr(sPath + sFileName) And vbDirectory) = vbDirectory Then
            If sFileName <> "." And sFileName <> ".." Then
                ReDim Preserve sChildDir(lDirNum) As String
                sChildDir(lDirNum) = sFileName
                lDirNum = lDirNum + 1
            End If

        Else

            If UCase$(FindName) = UCase$(sFileName) Then
            Text1 = sPath & sFileName
                'If MsgBox("已经找到文件、存在于目录:" & sPath & vbCrLf & "是否停止查找并运行?", vbQuestion + vbYesNo) = vbYes Then
                    'Call Shell("cmd /c start " & sPath & sFileName, vbHide)
               
                    'Exit Function
               ' End If
            End If
            
        End If

        sFileName = Dir
    Wend
   
    For lCount = 0 To lDirNum - 1
        Call SearchPath(sPath + sChildDir(lCount), FindName)
    Next
   
    Erase sChildDir
    SearchPath = True
    Exit Function
SearchPathErr:
    SearchPath = False
End Function

Private Sub Command1_Click()
SearchPath "c:\", "小明.txt"'不知道怎么设置不知道怎么将List1的全部(全部哦!可以声明:i 依次扫描..)收索文件名为收索对象

End Sub

如果老大们觉得以上的代码不符合要求请大家帮我想想这个问题吧!要的目的是:将List1的全部(全部哦!可以声明:i 依次扫描..)收索文件名为收索对象
'收索到了就会将文件的路径显示在List2的里面!
1 回复
#2
风吹过b2012-03-23 20:47
你找到的代码是适用于搜索具体的文件的代码,不适用于你这种的 全盘搜索并添加所有的文件的方式。
这种的全盘搜索并添加所有的文件,最好容易看得懂的代码是 递归 。以前写过一个,代码丢了。
到这里只大概用中文描述一下算法。

Private sub ADDFILE(ByVal sPath As String, ByVal FindName As String, obj as listbox2) As Boolean
'spath 搜索路径
'findname 要搜索的文件名或后缀
'obj 文件列表

dim dirlist() as string     '保存目录用
dim i as long ,j as long ,s as string

'-----------
'搜索当前目录存在多少符合条件的文件
'每找到一个就添加
'-----------
s=dir(spath & "\" & findname)
do while len(s)>0         's不为空
  obj.additme spath & "\" & s
  s=dir()
loop

'-------------
'对当前目录中的子目录进行计数
'代码略,在你的代码中可以找到对应的代码,稍改一下

redim dirlist(j)
'-------------
'保存当前目录下的目录的全路径
'代码略,与上一节基本相同

'--------------
'递归调用本身,搜索子目录
for i=1 to j
   call ADDFILE(dirlist(i),findname,obj)
next i

'当搜索完了所有的子目录后会返回上一层调用执行循环,结束后就结束整个过程的执行。
end sub
1