I feel really stupid posting this question, because I know I saw it somewhere before, just couldn't remember where!
Here's my situation:
-------------------
I already have:
- a listbox named "List1"
- a "Form_Load" sub which calls the "loadorrefreshdata" sub
I need some code to do the following:
- Get all the sub-folders in a specified directory ("testdir")
- Get the names of these subfolders (not the Directory.GetDirectories, as this returns the full path eg. "C:\Program Files\demofolder1\demosubfolder", where I only want "demosubfolder" as name.
- Populate List1 with these names
What would be the best way to do this
Thanks in advance!
Johannes

Directories?
Jay Hancock
sunnny
Dim theFolders() as String = Directory.GetDirectories("path\directoryname")
for each currentFolder as String in theFolders
Me.theListBox.Items.Add(Path.GetDirectoryName(currentFolder))
next
does this work for you
Dmytro Kryvko
whoops thats my fault, didnt have my coffee there. My apologies. Change IndexOf to LastIndexOf
so overall:
Dim theFolders() as String = Directory.GetDirectories("path\directoryname")
for each currentFolder as String in theFolders
Me.theListBox.Items.Add(currentFolder.SubString(currentFolder.LastIndexOf("\")))
next
gabemejia
Where the specified folder contains say about 50 sub-folders, I receive a list of about 50 entries, all of them named "60". This is how "List1" looks:
60
60
60
60
60
...
Any ideas what could be wrong
Thanks for all your help so far!
Johannes
Hugo de Oude
I just noticed another small problem: When the code is executed, it gets the parent directory of the subfolder...
Johannes
SukhiNew
Thanks for your reply! I've got a small problem though - the directories' full paths appear in the listbox.
Thanks!
Johannes
Moish
not sure I follow, you are after just the last directory correct You could use a substring:
Dim theLastFolderOnly as String = currentFolder.SubString(currentFolder.IndexOf("\"))
a9192shark
Sorry I took so long to reply. The code you sent me works perfectly - thanks!
Hope you're having a better day today lol
Johannes
swg
yes....it should be:
Me.theListBox.Items.Add(currentFolder.SubString(currentFolder.LastIndexOf("\")))
not having a great day this end! :-)
what the substring does is that it will get you a string from a specified position with an option length to get the string from.
Mark Frank
Thanks, that adds all the folder in the specific directory, however, now I get the full path *minus* the "C:", eg. "\Program Files\Directory\SubDirectory\thelastFolderonly". I'm really sorry to ask you again
Thanks
Johannes