试试 下面的:
=========================================================
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Const MaxLFNPath = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const VBKEYDOT = 46
Private Const VBBACKSLASH = "\"
Private Const VBALLFILES = "*.*"
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MaxLFNPath
cShortFileName As String * 14
End Type
Private WFD As WIN32_FIND_DATAOption Explicit
Public Function SearchDirs(Curpath$, strFName$)
Dim strProg$
Dim dirs%
Dim dirbuf$()
Dim hItem&
Dim i%
Dim rtn As Boolean
If Curpath$ = "" Then Exit Function
If strFName$ = "" Then Exit Function
If Right(strFName$, 1) = VBBACKSLASH Then
strFName = Left(strFName, InStr(1, strFName, VBBACKSLASH, vbTextCompare) - 1)
End If
If Right(Curpath$, 1) <> VBBACKSLASH Then
Curpath$ = Curpath$ & VBBACKSLASH
End If
hItem& = FindFirstFile(Curpath$ & VBALLFILES, WFD)
If hItem& <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> VBKEYDOT Then
If (dirs% Mod 10) = 0 Then ReDim Preserve dirbuf$(dirs% + 10)
dirs% = dirs% + 1
dirbuf$(dirs%) = Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
End If
End If
strProg$ = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
If UCase(strProg$) = UCase(strFName$) Then
SearchDirs = True
Exit Function
Else
SearchDirs = False
End If
DoEvents
Loop While FindNextFile(hItem&, WFD)
Call FindClose(hItem&)
End If
For i% = 1 To dirs%
rtn = SearchDirs(Curpath$ & dirbuf$(i%) & VBBACKSLASH, strFName$)
SearchDirs = rtn
If rtn Then Exit Function
Next i%
End Function
==========================================
=========================================================
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Const MaxLFNPath = 260
Private Const INVALID_HANDLE_VALUE = -1
Private Const VBKEYDOT = 46
Private Const VBBACKSLASH = "\"
Private Const VBALLFILES = "*.*"
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MaxLFNPath
cShortFileName As String * 14
End Type
Private WFD As WIN32_FIND_DATAOption Explicit
Public Function SearchDirs(Curpath$, strFName$)
Dim strProg$
Dim dirs%
Dim dirbuf$()
Dim hItem&
Dim i%
Dim rtn As Boolean
If Curpath$ = "" Then Exit Function
If strFName$ = "" Then Exit Function
If Right(strFName$, 1) = VBBACKSLASH Then
strFName = Left(strFName, InStr(1, strFName, VBBACKSLASH, vbTextCompare) - 1)
End If
If Right(Curpath$, 1) <> VBBACKSLASH Then
Curpath$ = Curpath$ & VBBACKSLASH
End If
hItem& = FindFirstFile(Curpath$ & VBALLFILES, WFD)
If hItem& <> INVALID_HANDLE_VALUE Then
Do
If (WFD.dwFileAttributes And vbDirectory) Then
If Asc(WFD.cFileName) <> VBKEYDOT Then
If (dirs% Mod 10) = 0 Then ReDim Preserve dirbuf$(dirs% + 10)
dirs% = dirs% + 1
dirbuf$(dirs%) = Left$(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
End If
End If
strProg$ = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
If UCase(strProg$) = UCase(strFName$) Then
SearchDirs = True
Exit Function
Else
SearchDirs = False
End If
DoEvents
Loop While FindNextFile(hItem&, WFD)
Call FindClose(hItem&)
End If
For i% = 1 To dirs%
rtn = SearchDirs(Curpath$ & dirbuf$(i%) & VBBACKSLASH, strFName$)
SearchDirs = rtn
If rtn Then Exit Function
Next i%
End Function
==========================================
本示例使用 Dir 函数来检查某些文件或目录是否存在。在 Macintosh 计算机上,默认驱动器名称是 “HD” ,并且路径部分由冒号取代反斜线隔开。而且 Microsoft Windows 的通配符在 Mac 中可以作为有效字符出现在文件名中。也可以使用 MacID 函数来指定文件组。Dim MyFile, MyPath, MyName' 返回“WIN.INI” (如果该文件存在)。
MyFile = Dir("C:\WINDOWS\WIN.ini") ' 返回带指定扩展名的文件名。如果超过一个 *.ini 文件存在,
' 函数将返回按条件第一个找到的文件名。
MyFile = Dir("C:\WINDOWS\*.ini")' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.ini 文件。
MyFile = Dir' 返回找到的第一个隐式 *.TXT 文件。
MyFile = Dir("*.TXT", vbHidden)' 显示 C:\ 目录下的名称。
MyPath = "c:\" ' 指定路径。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。
End If
End If
MyName = Dir ' 查找下一个目录。
Loop'以上是可以遍历所有目录查找那个您想要的文件。