|
foldername = qtpdir
filename ="标准版测试用例_new.xls"
y = FindFilePath(foldername,filename)
msgbox y '为什么这里打印为空
'在foldername文件夹及其子文件夹下找到一个名字为filename的文件的路径
Function FindFilePath(foldername,filename)
Dim fso,UtilFolder,UtilFolderCollection
Dim Find
Dim tpath
find = False
Set fso = CreateObject("Scripting.FileSystemObject")
On error resume next
Set UtilFolder=fso.GetFolder(foldername)
If UtilFolder is nothing Then
msgbox "FindFilePath的foldername参数不正确"
End If
'搜索当前所有子目录
For each f in UtilFolder.files
If StrComp(Lcase(f.name),Lcase(trim(filename))) = 0 Then
find = True
tpath = f.path
Exit For
End If
Next
If not find Then
Set UtilFolderCollection = UtilFolder.SubFolders
If Not UtilFolderCollection Is Nothing Then '如果子目录不为空
For each ufolder in UtilFolderCollection
FindFilePath ufolder,filename
Next
End If
Else
FindFilePath = CStr(tpath)
MsgBox "haha:" & FindFilePath '只打印一次,FindFilePath能取到路径
End If
Set UtilFolder = Nothing
Set UtilFolderCollection = Nothing
Set fso = Nothing
End Function |
|