Search code examples
vb.netvisual-studio-2012getdirectories

Returning subfolder names within dated folder


I'm trying to pull back a list of directories that sit inside a dated folder structure. Within each dated folder could be a number of 'Jobs' however i only want to return the name of the 1st level of folders

The below code gets to the right level of folder detail however the result displays the full path

 For Each Dir As String In System.IO.Directory.GetDirectories("c:\Working")
        Dim dirInfo As New System.IO.DirectoryInfo(Dir)
        For Each sDir As String In System.IO.Directory.GetDirectories(dirInfo.ToString)
            Dim sdirInfo As New System.IO.DirectoryInfo(sDir)
            chkImpExp.Items.Add(sDir)
        Next
    Next

This would display the following

Folder Structure

However i would just like to display the directory name to the right of the 3rd backslash (Westdale - 28023 - Cash+Spirit for example)

Hopefully this is enough information.

Many thanks


Solution

  • ' renamed Dir to d as Dir() is already a function in Microsoft.VisualBasic
    For Each d In System.IO.Directory.GetDirectories("c:\Working")
        For Each sDir In System.IO.Directory.GetDirectories(d)
            Dim di = New DirectoryInfo(sDir)
            chkImpExp.Items.Add(di.Name())
        Next
    Next