LuckyLuke82
LuckyLuke82

Reputation: 604

Add Subfolder names to Listview

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 ?

Upvotes: 0

Views: 320

Answers (1)

soohoonigan
soohoonigan

Reputation: 2350

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

Upvotes: 1

Related Questions