Directories?

Hi all

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



Answer this question

Directories?

  • Jay Hancock

    indeed! :-) Thanks very much and glad to know its working ;-)

  • sunnny

  • get all the sub-folders in a specified directory:

    Dim theFolders() as String = Directory.GetDirectories("path\directoryname")

  • get the names of these subfolders, just the name only.

    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

    Somehow, I now get a weird outcome: I receive a list with the same number of folders in the main folder, but each of them named "60". That probably didn't make sense, did it I'll try to explain below:

    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

    Hi

    I just noticed another small problem: When the code is executed, it gets the parent directory of the subfolder...

    Johannes


  • SukhiNew

    Hi!

    Thanks for your reply! I've got a small problem though - the directories' full paths appear in the listbox. I still get the "C:\Program Files\Directory\Directory..." path. Is there a way to trim this path so that I only get "Directory"

    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

    Hi

    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

    Hi

    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 , how to get just the last folder, which in the above case would be "thelastFolderonly".

    Thanks

    Johannes


  • Directories?