【问题标题】:How do I open a file if I only know part of the file name?如果我只知道文件名的一部分,如何打开文件?
【发布时间】:2010-05-18 20:17:04
【问题描述】:

我需要打开一个我不知道完整文件名的文件。

我知道文件名是这样的。

filename*esy

我确定在给定目录中该文件只出现一次。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我正在尝试将这个问题作为一个函数。这是最终为我工作的解决方案。

    Function fileName(path As String, sName As String, ext As String) As Variant
    
    'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
    'sName is the string to search. ? and * are wildcards.  ? is for single char
    'example sName = "book?" or sName ="March_*_2014*"
    'ext is file extention ie .pdf .xlsm .xls? .j*
    
    Dim file As Variant 'Store the next result of Dir
    Dim fname() As String 'Dynamic Array for result set
    ReDim fname(0 To 0) 
    Dim i As Integer ' Counter
    i = 0
    
    ' Use dir to search and store first result
    fname(i) = path & Dir(path & "\" & sName & ext) 
    i = i + 1
    
    'Load next result
    file = Dir 
    
    While file <> "" 'While a file is found store that file in the array
      ReDim Preserve fname(0 To i) As String
      fname(i) = path & file
      file = Dir
    Wend
    
    fileName = Application.Transpose(fname) 'Print out array
    
     End Function
    

    这适用于我作为单个或数组函数。

    【讨论】:

      【解决方案2】:

      filename*esy 已经是“shell 就绪”通配符,如果总是这样,您可以简单地;

      const SOME_PATH as string = "c:\rootdir\"
      ...
      Dim file As String
      file = Dir$(SOME_PATH & "filename*esy" & ".*")
      
      If (Len(file) > 0) Then
        MsgBox "found " & file
      End If
      

      只需调用(或循环直到为空)file = Dir$() 即可获得下一场比赛。

      【讨论】:

        【解决方案3】:
        If InStr(sFilename, "filename") > 0 and InStr(sFilename, "esy") > 0 Then
         'do somthing
        end if
        

        或者你可以使用正则表达式

         Dim RE As Object, REMatches As Object 
        
            Set RE = CreateObject("vbscript.regexp") 
            With RE 
                .MultiLine = False 
                .Global = False 
                .IgnoreCase = True 
                .Pattern = "filename(.*)esy" 
            End With 
        
            Set REMatches = RE.Execute(sFilename) 
            REMatches(0) 'find match
        

        【讨论】:

        • 出于某种原因,我假设文件已打开。哎呀
        【解决方案4】:

        您可以使用Application.FileSearch(见下文)。您可以使用它来搜索与您的模式匹配的文件。此信息取自here。

        Sub App_FileSearch_Example()
        
            With Application.FileSearch
                .NewSearch
                .LookIn = "c:\some_folder\"
                .FileName = "filename*esy"
                If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then    
                    For i1 = 1 To .FoundFiles.Count
                        ' do something with matched file(s)
                    Next i1
        
                End If
        
            End With    
        End Sub
        

        【讨论】:

          【解决方案5】:

          如果您知道没有其他文件按顺序包含“文件名”和“esy”,那么您可以简单地使用

          Workbooks.Open Filename:= "Filepath\filename*esy.*"
          

          或者,如果您知道丢失的字符数,那么(假设 4 个字符未知)

          Workbooks.Open Filename:= "Filepath\filename????esy.*"
          

          我使用这种方法在带有日期和时间戳的文件上运行代码以忽略时间戳部分。

          【讨论】:

            猜你喜欢
            • 2014-07-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-11
            • 1970-01-01
            • 2014-09-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多