Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb.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?

like image 317
Asynchronous Avatar asked Oct 16 '13 17:10

Asynchronous


4 Answers

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.

like image 81
varocarbas Avatar answered Oct 01 '22 10:10

varocarbas


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
like image 37
ElektroStudios Avatar answered Oct 02 '22 10:10

ElektroStudios


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
like image 29
DB Pros Avatar answered Oct 03 '22 10:10

DB Pros


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

like image 45
Michael Cooley Avatar answered Oct 03 '22 10:10

Michael Cooley