List Folderames in a folder

How do I list the names of all the folders inside another folder


Answer this question

List Folderames in a folder

  • Darathar

    no worries, glad I could help!

  • Justin Tang

    you can either do a remove:

    currentFolder.Remove("\\", "")

    or

    Me.theListBox.Items.Add(currentFolder.SubString(currentFolder.LastIndexOf("\") + 1))

    safer would be the first one :)



  • Eng. Habeeli

    Take a look at the directoryInfo class's GetDirectories method.


  • Aurrin

    Thanks Ken,
    Had a look, copied and pasted the code example and altered it as below but I still don't see how to get the names of the folders when it's run I just get the number 11 in the listbox, I guess that's because I have .Length but I can not see .Name anywhere and if I leave it off and run the program I get FileInfo[]Array in the listbox.
    I've decided that the MS Helpfiles are not that Helpful, if they were we would not have all these .Net forums on the web to Help us understand .Net, Ok moan over.

    Dim di As DirectoryInfo = New DirectoryInfo("C:\MyFolder\")

    Dim dirs As DirectoryInfo() = di.GetDirectories()
    Dim diNext As DirectoryInfo

    For Each diNext In dirs
    lstFolders.Items.Add(diNext.GetFiles().Length)
    Next



  • mqsash

    Thanks again Ahmedilyas,
    The second worked
    Me.lstTheme.Items.Add(currentFolder.Substring(currentFolder.LastIndexOf("\") + 1))

    The first one did not
    Me.lstTheme.Items.Add(currentFolder.Remove("\\", ""))
    Gave this error message
    Conversion from string "\\" to type 'Integer' is not valid.

  • Rafael Leonhardt

    Thanks Spotty,
    Like that, one liner, problem is that in the listbox I'm showing
    C:\MYFOLDER\Folder1
    C:\MYFOLDER\Folder2
    C:\MYFOLDER\Folder3

    What I want to show is just the folder name not the full path
    Folder1
    Folder2
    Folder3


  • Ramanujam Sampath

    You can simplify this to a single line

    Me.ListBox1.DataSource = My.Computer.FileSystem.GetDirectories("C:\MYFOLDER\")


  • Hila123

    Thanks for the reply this is as close as I've been to what I want,
    Here's what I'm getting now, how do I remove the backslash \

    \Folder1
    \Folder2
    \Folder3


  • windoze

    Just wondered if anyone has figured an answer yet

  • Scott Anthony

    Okey, Dokey, That gone and done it!

    Me.lstTheme.Items.Add(currentFolder.Replace("C:\MYFOLDERS\", ""))

    Cheers
    Ahmedilyas

  • N3UD

    woops! having bad days here, it should be Replace, not remove (silly me!) my apologese - again, its better to use the REPLACE method than the second approach

  • Kevin8264

    you would pretty much do a string parsing process:

    Dim theFolders() as String

    theFolders = Directory.GetDirectories(path)

    for each currentFolder as string in theFolders

    Me.theListBox.Items.Add(currentFolder.SubString(currentFolder.LastIndexOf("\"))

    next

    that should do it hopefully.

    Another way would be:

    Path.GetDirectoryName(currentFolder)

    in the for each loop

    does this help



  • List Folderames in a folder