vb.net 如何在目录中查找或获取文件名 Visual Basic.net 中带有特定单词的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19409522/
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-09 17:14:03  来源:igfitidea点击:

How to find or get files in Directory with Specific word in the file name Visual Basic.net?

vb.net

提问by Asynchronous

I need to get files from a directory containing specific characters in it's name: The following code below will return any file with the .csvextension. The problem is there are other csv file I need to leave alone or not get.

我需要从名称中包含特定字符的目录中获取文件:下面的代码将返回任何带有.csv扩展名的文件。问题是我需要单独留下或不获取其他 csv 文件。

Dim FileLocation As DirectoryInfo = _
 New DirectoryInfo("C:\Folder\Subfolder\Data\Input\")

Dim fi As FileInfo() = FileLocation.GetFiles("*.csv")

Instead of getting any csv file, I would like to get a file with the word data, so any file name containing the word data. Example: *my_data_file.csv*

相反得到任何csv文件,我想获得这个词是一个文件数据,因此包含文字数据的文件名。示例:*my_data_file.csv*

How do I do this with the code above?

我如何用上面的代码做到这一点?

采纳答案by varocarbas

You can update the filter with the string you want to account for (caps will automatically be taken care of):

您可以使用要考虑的字符串更新过滤器(将自动处理大写):

Dim fi As FileInfo() = FileLocation.GetFiles("*data*.csv")

In any case, bear in mind that this filtering is not "too accurate". For example, the code above would also account for any file (including "data"), whose extension includescsv(e.g., *.csva, *.csvb, etc.). If you want a 100%-reliable approach you should better set up a loop and carry out the filtering "manually"; loops are pretty fast and you wouldn't even notice the difference.

无论如何,请记住这种过滤不是“太准确”。例如,上面的代码还将说明任何文件(包括“数据”),其扩展名包括csv(例如,*.csva、*.csvb 等)。如果你想要一个 100% 可靠的方法,你最好设置一个循环并“手动”执行过滤;循环非常快,您甚至不会注意到差异。

Example of a loop:

循环示例:

Dim fi As List(Of FileInfo) = New List(Of FileInfo)
For Each File In FileLocation.GetFiles()
    If (File IsNot Nothing) Then
        If (Path.GetExtension(File.ToString.ToLower) = ".csv") Then
            If (File.ToString.ToLower.Contains("data")) Then fi.Add(File)
        End If
    End If
Next

This code will work for sure under your exact requirements and might take care of more complex requests. I have accounted for a Listjust to show the point clearer.

此代码肯定会在您的确切要求下工作,并且可能会处理更复杂的请求。我已经占了一个List只是为了更清楚地表明这一点。

回答by ElektroStudios

If you can use LINQ extensions then you can do it this way:

如果您可以使用 LINQ 扩展,那么您可以这样做:

' Get Files {directory} {recursive} {ext} {word in filename}
Private Function Get_Files(ByVal directory As String, _
                           ByVal recursive As IO.SearchOption, _
                           ByVal ext As String, _
                           ByVal with_word_in_filename As String) As List(Of IO.FileInfo)

    Return IO.Directory.GetFiles(directory, "*" & If(ext.StartsWith("*"), ext.Substring(1), ext), recursive) _
                       .Where(Function(o) o.ToLower.Contains(with_word_in_filename.ToLower)) _
                       .Select(Function(p) New IO.FileInfo(p)).ToList

End Function

Usage example:

用法示例:

    For Each file As IO.FileInfo In Get_Files("C:\Folder\Subfolder\Data\Input\", _
                                              IO.SearchOption.TopDirectoryOnly, _
                                              "csv", _
                                              "data")
        MsgBox(file.Name)

    Next

回答by Michael Cooley

I would have added this as a comment to the accepted answer, but I do not have enough points to do so:

我会将此作为评论添加到已接受的答案中,但我没有足够的积分来这样做:

I just wanted to add varocarbas's answer that, if anyone was wondering (as I was) if this would work in a web scenario as well, it will. Just place the web path inside Server.MapPath()like this:

我只是想补充一下 varocabas 的回答,如果有人想知道(就像我一样)这是否也适用于网络场景,它会。只需Server.MapPath()像这样将网络路径放在里面:

Dim FileLocation As DirectoryInfo = 
 New DirectoryInfo(Server.MapPath("/Folder/SubFolder/Data/Input/"))

NOTE:Will NOT work with full url's (no 'http://www.123.com').

注意:不适用于完整网址(没有“ http://www.123.com”)。

回答by DB Pros

Replace the wildcard search below "." with your search criteria, for example you want all files that start with name "Hospital*"

将“ .”下面的通配符搜索替换为您的搜索条件,例如您希望所有以名称“Hospital*”开头的文件



Dim Folder As New IO.DirectoryInfo("C:\SampleFolder")

For Each File as IO.FileInfo in Folder.GetFiles("*.*",IO.SearchOption.AllDirectories)
       ListBox1.Items.Add(File.FullName)
Next

回答by Vincent 13th

Dim Folder As New IO.DirectoryInfo("C:\SampleFolder")
For Each File as IO.FileInfo in Folder.GetFiles("*.*",IO.SearchOption.AllDirectories)
    ListBox1.Items.Add(File.FullName)
    Application.DoEvents()
Next