27 | 06 | 2017

Search file in directory by name

Using the function below it's possible to search for a file within a given directory. If found it returns the path. In this example it's not needed to give the complete file name. The function searched for the given string within the filename.

 

    Public Function GetFileLink(ByVal targetDirectory As String, ByVal Namepart As String) As String
        GetFileLink = ""

        Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory. 

        Dim fileName As String
        For Each fileName In fileEntries
            If StrConv(fileName, vbUpperCase) Like StrConv("*" & Namepart & "*", vbUpperCase) Then
                'Iterate through the files in the given directory and return the link
                GetFileLink = fileName
                Exit Function
            End If
        Next fileName

    End Function 'ProcessDirectory
 
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2288643
Latest Articles