我需要通过使用VB脚本搜索来返回文件的完整路径。例如,这是我的文件夹:
我想在C:\目录中搜索名为“sample1.txt”的文件并回显它。
输出为“ C:\ test \ test1 \ sample1.txt ”
答案 0 :(得分:0)
dir /s /b
并捕获/处理输出答案 1 :(得分:0)
稍微调整一下:
Const fileName = "cFiles.vbs" 'Filename to search
Set fso = CreateObject("Scripting.FileSystemObject")
dir = "C:\Users\makoy\Documents\CommonFiles" 'Place directory to search
If fso.FolderExists(dir) Then _
file = FindFile(LCase(fileName), fso.GetFolder(dir))
If Len(file) = 0 Then
WScript.Echo "Error: File Not Found"
WScript.Quit 2
End If
Set folder = fso.GetFolder(file & "\..")
WScript.Echo folder & "\" & fileName
WScript.Quit
Function FindFile(ByRef sName, ByRef oFolder) 'As String
FindFile = ""
For Each file In oFolder.Files
If LCase(file.Name) = sName Then
FindFile = file
Exit Function
End If
Next 'file
For Each dir In oFolder.SubFolders
FindFile = FindFile(sName, dir)
If Len(FindFile) Then _
Exit Function
Next 'dir
End Function