vba 如果我只知道文件名的一部分,如何打开文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2860797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:27:28  来源:igfitidea点击:

How do i open a file if i only know part of the file name?

excelvbaexcel-vba

提问by l--''''''---------''''''''''''

I need to open a file whose full filename I do not know.

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

I know the file name is something like.

我知道文件名是类似的。

filename*esy

I know definitely that there's only one occurrence of this file in the given directory.

我肯定知道在给定目录中这个文件只出现一次。

回答by Alex K.

filename*esyis already a "shell ready" wildcard & if thats alway the case you can simply;

filename*esy已经是一个“shell ready”通配符,如果总是这样,你可以简单地;

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

Just call (or loop until empty) file = Dir$()to get the next match.

只需调用(或循环直到为空)file = Dir$()即可获得下一个匹配项。

回答by Glennular

If InStr(sFilename, "filename") > 0 and InStr(sFilename, "esy") > 0 Then
 'do somthing
end if

Or you can use RegEx

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

 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

回答by dcp

There is an Application.FileSearchyou can use (see below). You could use that to search for the files that match your pattern. This information taken from here.

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

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

回答by Rex

I was trying this question as a function. This is the solution that ended up working for me.

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

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

This works for me as a single or array function.

这对我来说是一个单一或数组函数。

回答by Andrew McGregor

If you know that no other file contains "filename" and "esy" in that order then you can simply use

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

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

Or if you know the number of missing characters then (assuming 4 characters unknown)

或者,如果您知道缺失字符的数量(假设有 4 个字符未知)

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

I use this method to run code on files which are date & timestamped to ignore the timestamp part.

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