【问题标题】:Getting full path of a file获取文件的完整路径
【发布时间】:2014-11-26 09:08:45
【问题描述】:

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

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

输出将是“C:\test\test1\sample1.txt

【问题讨论】:

  • 返回什么?你打算对路径做什么?
  • 把它放在一个变量中。或者只是回应它。

标签: file search vbscript find


【解决方案1】:

【讨论】:

    【解决方案2】:

    从这里得到答案: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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-31
      • 2012-10-19
      • 2018-10-24
      • 2015-11-03
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多