0

I have Combobox, and I select folder names in It. This folder has to be searched first. In these folders are also folders named "Versions"- and these folders have another folders which I need to add on Listview. I tried this but nothing is added to my Listview:

Dim Folder_To_Search As String() = IO.Directory.GetDirectories("D:\", MyCombo.Text, System.IO.SearchOption.AllDirectories)

            For Each folder As String In Folder_To_Search
               ListView1.Items.Add(Path.GetFileName(folder + "\Versions\"))
            Next

I guess I'm missing something after + "\Versions\", can somebody give me a clue ?

LuckyLuke82
  • 586
  • 1
  • 18
  • 58
  • `Path.GetFileName()` gets the file name from the string passed - you are passing a path or partial path. Are you trying to get the filenames from that path? – Ňɏssa Pøngjǣrdenlarp Sep 28 '16 at 16:57
  • @Plutonix yes, foldernames from e.g. D:\Microsoft\Versions\... – LuckyLuke82 Sep 28 '16 at 17:00
  • Please don't concatenate strings with the plus (`+`), it is mainly the addition operator. VB.NET uses the ampersand (`&`) as its native concatenation operator. By using it you will avoid getting into problems such as the ones described [here](http://stackoverflow.com/a/734631/3740093). – Visual Vincent Sep 28 '16 at 17:01
  • @VisualVincent, thanks for info. I just need get this working so I tried with "+". "&" doesn't work either though.... – LuckyLuke82 Sep 28 '16 at 17:10
  • Sorry for being unclear... It wasn't your problem in this case, but by using the ampersand instead you avoid future problems. – Visual Vincent Sep 28 '16 at 19:53

1 Answers1

1

Nothing is being added to your listview because GetDirectories returns, as its name implies, directories. So you're getting your list of directories and then using Path.GetFilename on each of them, but the directories do not have a filename at the end of them so only empty strings are being added to your listview.


Edit for Comment: Then it sounds like you need to run basically two nested directory searches; the first one for folders like "Microsoft" and the second for "Versions" within Microsoft folders, then loop through and get the files:

    Dim TopLevelDirectories As String() = IO.Directory.GetDirectories("D:\", "*" & MyCombo.Text & "*", System.IO.SearchOption.AllDirectories)
    For Each tlDir As String In TopLevelDirectories
        Dim SubLevelDirectories As String() = IO.Directory.GetDirectories(tlDir, "*Versions*", System.IO.SearchOption.AllDirectories)
        For Each slDir As String In SubLevelDirectories
            Dim dInfo As DirectoryInfo = New DirectoryInfo(slDir)
            Dim fInfo() As FileInfo = dInfo.GetFiles
            For Each f As FileInfo In fInfo
                ListView1.Items.Add(f.FullName) 'or ListView1.Items.Add(f.Name)
            Next
        Next
    Next

If I understand your goal correctly, the code above should find all the files you're looking for. I made some test folders and threw Microsoft/Versions in at different levels of the directories and this code picked them all up

soohoonigan
  • 2,342
  • 2
  • 10
  • 18
  • thanks for reply and explanation. I see your point now. However your code doesn't work. I didn't quite elaborate: "Microsoft" isn't located in "D:\Microsoft" only, It can be located in "D:\SomeFolder\Microsoft\" too. So folder Microsoft must be searched first, that's why I use SearchOption.Alldirectories. Combobox concatenation like yours doesn't do that. – LuckyLuke82 Sep 29 '16 at 10:06
  • Can't you search for the location of "Microsoft" first and then put the location in a variable and then carry on with the code in the answer? – Cal-cium Sep 29 '16 at 10:41
  • I misunderstood your goal, I've edited the code and I think it will work for what you're trying to accomplish now – soohoonigan Sep 29 '16 at 13:04