Home > Net >  In Visual Basic how do I make a list of subfolders underneath a folder I have a path for when loadin
In Visual Basic how do I make a list of subfolders underneath a folder I have a path for when loadin

Time:01-27

I have a file structure for VB as initial folder
sub 1 folder
sub sub folder 1
SUB SUB FOLDER 2
. . . sub n folder

I want to put the name of 1 through n folder as items in a strip menu and save the paths to be used later to access the files in the sub folders. I only want the sub xxx names and not the sub SUB names

I tried this

    Dim appPath As String = Application.StartupPath()
    Dim n As Integer, patt As String

    '  Dim DirectoryList As ArrayList
    '  Dim listbox1 As String
    n = InStr(appPath, "bin")
    patt = LSet(appPath, n - 1)
    TextBox2.Text = n
    TextBox1.Text = appPath
    TextBox3.Text = patt

    Dim folderPath As String
    Dim directorylist As ArrayList

    ListBox1.Items.Clear()

    Dim folderpath = My.Computer.FileSystem.GetFiles(patt, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")

    For Each foldername As System.IO.Path In folderPath
        ListBox1.Items.Add(foldername)
    Next

CodePudding user response:

Have a look at System.IO.Directory

In your case you can try something like this:

Dim directoryList() As String = Directory.GetDirectories(patt, SearchOption.TopDirectoryOnly)

CodePudding user response:

I found how to do this to just get the names of the first level of subfolders. Here it is . Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim appPath As String = Application.StartupPath()
    Dim n, lengthpath As Integer, patt, patt1 As String

    '  Dim listbox1 As String
    n = InStr(appPath, "bin")
    patt = LSet(appPath, n - 14)   "Data\"
    TextBox2.Text = n
    TextBox1.Text = appPath
    TextBox3.Text = patt
    lengthpath = Len(patt)   1

    ListBox1.Items.Clear()
    Dim fileNames = My.Computer.FileSystem.GetDirectories(patt, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
    For Each fileName As String In fileNames
        patt1 = Mid(fileName, lengthpath)
        ListBox1.Items.Add(patt1)
    Next

End Sub
  • Related