‘ 转载 : https://blog.youkuaiyun.com/hywerr/article/details/70228086
' 递归函数如下
Function FileExsitInDirectory(fso As Object, filename As String, dirpath As String)
Dim curPath As String
curPath = dirpath & "\" & filename
Dim objset As Object
Dim objFolders As Object
Dim FolderName As String
Dim ret As String
FileExsitInDirectory = "NULL"
If (fso.FileExists(curPath)) Then
FileExsitInDirectory = curPath
Else
Set objset = fso.getFolder(dirpath)
Set objFolders = objset.SubFolders
For Each objFolder In objFolders
FolderName = dirpath & "\" & objFolder.Name & "\"
ret = FileExsitInDirectory(fso, filename, FolderName)
If ret <> "NULL" Then
FileExsitInDirectory = ret
Exit Function
End If
Next
End If
’用法如下:
Sub call1()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fname As String
fname = "img1.mm115.net_pic_1642_1.jpg"
Dim dname As String
dname = "D:\PRVIT"
MsgBox FileExsitInDirectory(fso, fname, dname)
'If (FileExsitInDirectory(fso, fname, dname) <> "Null") Then
' MsgBox fname & "exsit in " & dname
' End If
End Sub