2010-05-18 42 views

回答

13

filename*esy已经是一个“空壳准备”通配符&如果多数民众赞成送花儿给人的情况下,你可以简单地;

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$()拿到下一场比赛。

1
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 
+0

由于某种原因,我假设文件已打开。哎呀 – Glennular 2010-05-18 21:32:56

2

有一个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 
0

我正在尝试这个问题的功能。这是最终为我工作的解决方案。

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 

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

0

如果您知道任何其他文件中包含的顺序“文件名”和“ESY”,那么你可以简单地使用

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

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

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

我用这个方法来在其上日期&时间戳忽略时间戳部分文件运行代码。