Asynchronous
Asynchronous

Reputation: 3977

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

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 .csv extension. The problem is there are other csv file I need to leave alone or not get.

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*

How do I do this with the code above?

Upvotes: 6

Views: 69864

Answers (5)

Michael Cooley
Michael Cooley

Reputation: 33

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:

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').

Upvotes: 1

DB Pros
DB Pros

Reputation: 31

Replace the wildcard search below "." with your search criteria, for example you want all files that start with name "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

Upvotes: 2

Vincent 13th
Vincent 13th

Reputation: 21

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

Upvotes: 0

ElektroStudios
ElektroStudios

Reputation: 20464

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

' 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

Upvotes: 2

user2480047
user2480047

Reputation:

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 includes csv (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.

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 List just to show the point clearer.

Upvotes: 5

Related Questions