# Re: 搜寻档案!!

Posted by Windows NT user on March 30, 1999 at 18:12:47:

In Reply to: 搜寻档案!! posted by 阿亮 on March 30, 1999 at 14:56:10:

: 如何利用vb6.0来搜寻某档案,是否存在,及接收传回来的参数!!
: 多谢

'in modules
Option Explicit
Public FoundFile() As String '存放传回值的字串阵列
Private ntx As Long

Public Sub FindFile(StartPath As String, fileMask As String)
ntx = 0
Call GetDirPath(StartPath, fileMask)
End Sub

Private Sub GetDirPath(CurrentPath As String, ByVal SearFile As String)
Dim nI As Integer, nDirectory As Integer, i As Long
Dim sFileName As String, sDirectoryList() As String
'First list all normal files in this directory
sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)
Do Until sFileName = ""
If UCase(sFileName) Like UCase(SearFile) Then
i = GetAttr(CurrentPath + sFileName)
If (i And vbDirectory) = 0 Then
ReDim Preserve FoundFile(ntx)
FoundFile(ntx) = CurrentPath + sFileName
ntx = ntx + 1
End If
End If
If sFileName <> "." And sFileName <> ".." Then
On Error GoTo e
'Ignore nondirectories
If GetAttr(CurrentPath & sFileName) And vbDirectory Then

nDirectory = nDirectory + 1
ReDim Preserve sDirectoryList(nDirectory)
sDirectoryList(nDirectory) = CurrentPath & sFileName
End If
e:
End If
On Error Resume Next
sFileName = Dir
Loop
'Recursively process each directory
For nI = 1 To nDirectory
GetDirPath sDirectoryList(nI) & "\", SearFile
Next nI
End Sub

e.g:FindFile *.txt , c:\

Follow Ups: