获取文件的完整路径

时间:2014-11-26 09:08:45

标签: file search vbscript find

我需要通过使用VB脚本搜索来返回文件的完整路径。例如,这是我的文件夹:

enter image description here

我想在C:\目录中搜索名为“sample1.txt”的文件并回显它。

输出为“ C:\ test \ test1 \ sample1.txt

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

从这里得到答案:http://www.computing.net/answers/programming/vbscript-to-find-file-and-return-filtered-folder-path/28076.html

稍微调整一下:

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