Hi Group,

I attempting to save a file to a specific folder that is begins with the hotels property number. Currently I've manually listed the properties complete folder name in a text file. This is proving to be a management problem that needs to be fixed.

The code I'm using to find and save the file looks like this:

Using reader As New StreamReader("O:\IPSDATA\Property Folder Names.txt")
     While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        If line.Contains(propertyNo2) Then
            hotelFolder = line
            Exit While
        End If
     End While
End Using

' setting the path name to save the converted file
fileSave = "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\" & hotelFolder & "\Restran\" & propertyNo & "Restran.txt"

You can see a line that looks for the folder using "line.Contains". Is there a way I can do the same thing when I'm looking through a directory? In other words, how can I look through the directory, "O:\Revenue Management\Centralized Revenue Management Service\CRMS Hotels\", find a folder that begins with (as an example) "0213" and then return the full name of that folder in the form of a string?

I'll continue to search the internet. But if you know how, please speak up!

Thanks,

Don

Recommended Answers

All 2 Replies

Here are 3 methods that each accomplish this:

getFolderNameGD (System.IO.Directory.GetDirectories) - version 1:

Public Function getFolderNameGD(ByVal fullyQualifiedFolderName As String, ByVal searchPattern As String)

    'return directory that matches searchPattern
    For Each fqDirName As String In System.IO.Directory.GetDirectories(fullyQualifiedFolderName, searchPattern, System.IO.SearchOption.TopDirectoryOnly)
        Return fqDirName
    Next

    Return String.Empty
End Function

getFolderNameGDI (System.IO.DirectoryInfo) - version 2:

Public Function getFolderNameGDI(ByVal fullyQualifiedFolderName As String, ByVal searchPattern As String)

    Dim di As New System.IO.DirectoryInfo(fullyQualifiedFolderName)
    Dim dirInfoArr() As System.IO.DirectoryInfo = di.GetDirectories(searchPattern, System.IO.SearchOption.TopDirectoryOnly)

    'return directory that matches searchPattern
    For Each d As System.IO.DirectoryInfo In dirInfoArr
        Return d.FullName
    Next

    Return String.Empty
End Function

getFolderNameED (System.IO.EnumerateDirectories) - version 3:

Public Function getFolderNameED(ByVal fullyQualifiedFolderName As String, ByVal propertyNumber As String)

    'return directory that starts with 
    'property number followed by a space and a "-"
    For Each fqDirName As String In System.IO.Directory.EnumerateDirectories(fullyQualifiedFolderName)
        Dim dirName = fqDirName.Replace(System.IO.Path.GetDirectoryName(fqDirName) & "\", "")

        If dirName.Substring(0, dirName.IndexOf("-") - 1) = propertyNumber Then
            'Debug.WriteLine("dirName ED: " & dirName)
            Return fqDirName
        End If
    Next

    Return String.Empty
End Function

Usage:

'version 1
Dim foldernameGD As String = getFolderNameGD("C:\Temp\Revenue Management\Centralized Revenue Management Service\CRMS Hotels", "1504 -*")

'version 2
Dim foldernameGDI As String = getFolderNameGDI("C:\Temp\Revenue Management\Centralized Revenue Management Service\CRMS Hotels", "1504 -*")

'version 3
Dim foldernameED As String = getFolderNameED("C:\Temp\Revenue Management\Centralized Revenue Management Service\CRMS Hotels", "1504")

example folder name: "1504 - FPbs Meriden"

cgeier, I chose your first option. It worked like it was supposed to! Thanks for teaching me this. I knew there was something in there that would allow this. I just needed to be pointed in the right direction.

Thank you again!

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.