[RESOLVED] [2005] Get folder names only within specific directory?
The idea is that people enter their surname into a text box and press a button which searches for username folders contained within the Surname directory. This works but displays the full path of the immediate folders contained within the Surname directory. I know there is a GetFileNameOnly function. Is there something similar for folders? If not, what's my best way to do this? Pre-process the string before adding it by searching for characters after the last \ character?
VB Code:
Dim frmFinish As New frmAboutBox
If System.IO.Directory.Exists(Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\") Then
Dim Count As Integer = System.IO.Directory.GetDirectories _
(Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\").Length
Select Case Count
Case Is >= 2
MessageBox.Show("There are " & Count & " directories with that surname. Please select from below " _
& "from the list which username you would like to recover the password for.", "Count2", MessageBoxButtons.OK)
For Each FolderName As String In System.IO.Directory.GetDirectories _
(Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\")
lbxUserNames.Items.Add(FolderName) 'Adds full path, how do I get only
Next 'folder names within the UserAccounts/Surname/ directory?
Case Is = 1
'Load single username into listbox
'Blah
'Blah
Case Else
MessageBox.Show("Not enough folders to examine. More users needed", "Insufficient Users", MessageBoxButtons.OK)
frmFinish.Show()
Me.Hide()
End Select
End If
Re: [2005] Get folder names only within specific directory?
You can do a split on the path on the "\" character and then grab the last item in the array like so.
VB Code:
Dim arr As String()
Dim strTemp As String
If System.IO.Directory.Exists("C:\") Then
Dim Count As Integer = System.IO.Directory.GetDirectories _
("C:\").Length
Select Case Count
Case Is >= 2
MessageBox.Show("There are " & Count & " directories with that surname. Please select from below " _
& "from the list which username you would like to recover the password for.", "Count2", MessageBoxButtons.OK)
For Each FolderName As String In System.IO.Directory.GetDirectories("C:\")
arr = FolderName.Split("\")
strTemp = arr(arr.GetUpperBound(0))
lbxUserNames.Items.Add(strTemp) 'Adds full path, how do I get only
Next 'folder names within the UserAccounts/Surname/ directory?
Case Is = 1
'Load single username into listbox
'Blah
'Blah
Case Else
MessageBox.Show("Not enough folders to examine. More users needed", "Insufficient Users", MessageBoxButtons.OK)
'frmFinish.Show()
'Me.Hide()
End Select
End If
Re: [2005] Get folder names only within specific directory?
You can use IO.Path.GetFileName to just get the folder name at the end and nothing else...
VB Code:
For Each FolderName As String In System.IO.Directory.GetDirectories("c:\", "*", IO.SearchOption.TopDirectoryOnly)
MessageBox.Show(IO.Path.GetFileName(FolderName))
Next
Re: [2005] Get folder names only within specific directory?
Brilliant stuff. Thanks guys
Stim