Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Get folder names only within specific directory?

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [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:
    1. Dim frmFinish As New frmAboutBox
    2.         If System.IO.Directory.Exists(Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\") Then
    3.             Dim Count As Integer = System.IO.Directory.GetDirectories _
    4.             (Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\").Length
    5.             Select Case Count
    6.                 Case Is >= 2
    7.                     MessageBox.Show("There are " & Count & " directories with that surname. Please select from below " _
    8.                     & "from the list which username you would like to recover the password for.", "Count2", MessageBoxButtons.OK)
    9.                     For Each FolderName As String In System.IO.Directory.GetDirectories _
    10.                     (Application.StartupPath & "\User Accounts\" & txtRemindSurname.Text & "\")
    11.  
    12.                         lbxUserNames.Items.Add(FolderName)   'Adds full path, how do I get only
    13.                     Next                                     'folder names within the  UserAccounts/Surname/ directory?
    14.                 Case Is = 1
    15.                     'Load single username into listbox
    16.                     'Blah
    17.                     'Blah
    18.                 Case Else
    19.                     MessageBox.Show("Not enough folders to examine. More users needed", "Insufficient Users", MessageBoxButtons.OK)
    20.                     frmFinish.Show()
    21.                     Me.Hide()
    22.             End Select
    23.         End If
    Last edited by stimbo; Oct 2nd, 2006 at 10:40 AM.

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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:
    1. Dim arr As String()
    2.         Dim strTemp As String
    3.         If System.IO.Directory.Exists("C:\") Then
    4.             Dim Count As Integer = System.IO.Directory.GetDirectories _
    5.             ("C:\").Length
    6.             Select Case Count
    7.                 Case Is >= 2
    8.                     MessageBox.Show("There are " & Count & " directories with that surname. Please select from below " _
    9.                     & "from the list which username you would like to recover the password for.", "Count2", MessageBoxButtons.OK)
    10.                     For Each FolderName As String In System.IO.Directory.GetDirectories("C:\")
    11.                         arr = FolderName.Split("\")
    12.                         strTemp = arr(arr.GetUpperBound(0))
    13.                         lbxUserNames.Items.Add(strTemp)   'Adds full path, how do I get only
    14.                     Next                                     'folder names within the  UserAccounts/Surname/ directory?
    15.                 Case Is = 1
    16.                     'Load single username into listbox
    17.                     'Blah
    18.                     'Blah
    19.                 Case Else
    20.                     MessageBox.Show("Not enough folders to examine. More users needed", "Insufficient Users", MessageBoxButtons.OK)
    21.                     'frmFinish.Show()
    22.                     'Me.Hide()
    23.             End Select
    24.         End If
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. For Each FolderName As String In System.IO.Directory.GetDirectories("c:\", "*", IO.SearchOption.TopDirectoryOnly)
    2.             MessageBox.Show(IO.Path.GetFileName(FolderName))
    3. Next

  4. #4

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Get folder names only within specific directory?

    Brilliant stuff. Thanks guys

    Stim

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width