locked
sub folder / sub folders RRS feed

  • Question

  • Dim strDirs() As IO.DirectoryInfo = DI.GetDirectories()

    For Each subFolder As IO.DirectoryInfo In strDirs

    If dirBox1.Items.Count < (strDirs.Length + 1) Then

     

    dirBox1.Items.Add(subFolder)

    End If

     

    hello all

    this piece of code gets all the folders  in the my music folder and adds them to the dirbox1 dropdown list

    I then choose a folder and get the files in that folder.......

    my problem is this......

    some of these folders contain more sub folders which I need to open to get the files....

    can anyone tell me how I can check if a subfolder contains more sub folders,

     

    thanks

    clint

    Friday, January 4, 2008 10:07 AM

Answers

  • You might find using a TreeView gives it more structure.

     

    Add a TreeView to a new project and add this code if you want to give it a try.

     

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    GetFolders(Nothing, My.Computer.FileSystem.SpecialDirectories.MyMusic, TreeView1)

    End Sub

    Private Sub GetFolders(ByVal Node As TreeNode, ByVal Root As String, ByVal Tree As TreeView)

    If Node Is Nothing Then Node = Tree.Nodes.Add(Root, IO.Path.GetFileName(Root))

    Node.Tag = Root

    Dim Folders() As String = IO.Directory.GetDirectories(Root)

    Array.Sort(Folders)

    For Each Folder As String In Folders

    Dim NewNode As TreeNode = Node.Nodes.Add(Folder, IO.Path.GetFileName(Folder))

    GetFolders(NewNode, Folder, Tree)

    Next

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    Me.Text = TreeView1.SelectedNode.Name

    End Sub

    End Class

    Friday, January 4, 2008 1:39 PM
  • Hi FVxSF

     

    Thanks for that - although I don't think it's particularly original.

     

    I've come up with the following which doesn't use recursion, and only loads the top level nodes until you click on one then it loads the next level

     

    It should produce a much faster startup when you have a large drive but I haven't tested it as much as I would like.  Maybe you could test it for me and let me know if you find any problems.

     

    If you still need more explanation let me know and I will produce a commented version tomorrow.

     

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    GetFolders(Nothing, "c:\", TreeView1)

    End Sub

    Private Sub GetFolders(ByVal Node As TreeNode, ByVal Root As String, ByVal Tree As TreeView)

    If Node Is Nothing Then

    If Root.Length > 3 Then

    Node = Tree.Nodes.Add(Root, IO.Path.GetFileName(Root))

    Else

    Node = Tree.Nodes.Add(Root)

    End If

    End If

    Dim Folders() As String = IO.Directory.GetDirectories(Root)

    Array.Sort(Folders)

    For Each Folder As String In Folders

    Dim NewNode As TreeNode = Node.Nodes.Add(Folder, IO.Path.GetFileName(Folder))

    For Each SubFolder As String In IO.Directory.GetDirectories(Folder)

    NewNode.Nodes.Add("DummyFolder - We'll Never Show This")

    Exit For

    Next

    Next

    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

    If e.Node.Nodes(0).Text = "DummyFolder - We'll Never Show This" Then

    e.Node.Nodes.RemoveAt(0)

    GetFolders(e.Node, e.Node.Name, TreeView1)

    End If

    End Sub

    End Class

    Saturday, January 5, 2008 12:45 AM

All replies

  • Tell GetDirectiories you want it to search subdirectories.

    Friday, January 4, 2008 12:44 PM
  • You might find using a TreeView gives it more structure.

     

    Add a TreeView to a new project and add this code if you want to give it a try.

     

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    GetFolders(Nothing, My.Computer.FileSystem.SpecialDirectories.MyMusic, TreeView1)

    End Sub

    Private Sub GetFolders(ByVal Node As TreeNode, ByVal Root As String, ByVal Tree As TreeView)

    If Node Is Nothing Then Node = Tree.Nodes.Add(Root, IO.Path.GetFileName(Root))

    Node.Tag = Root

    Dim Folders() As String = IO.Directory.GetDirectories(Root)

    Array.Sort(Folders)

    For Each Folder As String In Folders

    Dim NewNode As TreeNode = Node.Nodes.Add(Folder, IO.Path.GetFileName(Folder))

    GetFolders(NewNode, Folder, Tree)

    Next

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    Me.Text = TreeView1.SelectedNode.Name

    End Sub

    End Class

    Friday, January 4, 2008 1:39 PM
  • thank you dave

    I have never used treeview before so I appreciate this code, I will experiment with it....

    but I would still like to know how to check if subfolder contains other subfolders

    I am searching a folder for files, but some folders do not contain files....they contain more folders which contain the files

    if you know what I mean 

     

    I load the folders in a dropdown list

    I click a folder...which adds the files to a listview....(((.if the files exist )))

    but sometimes they do exist.....but in a subfolder.....

    so now I need to search / load the subfolder to access the files.....

     

    is this called recursive.....???

     

    thanks

    clint

     

    btw......all the best for the new year dave

    hope its a good one......and thanks for all your help

    clint

    Friday, January 4, 2008 3:46 PM
  • Clint

     

    Happy New Year to you too.

     

    Whether you need recursion depends how many levels of folder you have.  If you just have two then listing the top level and then just getting the files for every subfolder of whichever one you click on would do.  But then if those subfolders have their own subfolders then you are into recursion.

     

    The routine I posted above for the treeview uses recursion so I suggest you have a look at that and see if you can adapt it for your needs.

     

    Alternatively you could look at the way John suggests above.  GetDirectories has an overload to retrieve sub directories as well - so for each top level directory you could gather all the subdirectories (including sub directories of the sub directories) and then get the files for each one you have gathered.

     

    Either way should work but I would suggest you have a look at the recursive method as it's a useful tool to have available, although if you get it wrong it's very easy to get stuck in a never ending loop.

     

    Good Luck.

     

    Friday, January 4, 2008 4:56 PM
  • dave

    I've just been playing around with the treeview.....I think I like it...... a lot

    thank you again

    all the best

    clint

    Friday, January 4, 2008 5:48 PM
  • Hey Dave! This is a nice bit of code. I've seen other code that take almost 200 lines to do what you've done. But I have a question. I too am some what new to treeviews. They are very promising. But from all the code I've seen, no one really explains their code either at all or poorly. I don't suppose you could give us a run down on how it works? Also. I've noticed that it will load all directories in to the tree view on load. I tested this on my H: drive and it took a while as i have hundreds of folders. Is there a way to list folder after the node is selected? I too have a similar need as clint 2 but i'm a n00b with treeviews
    Friday, January 4, 2008 7:44 PM
  • Hi FVxSF

     

    Thanks for that - although I don't think it's particularly original.

     

    I've come up with the following which doesn't use recursion, and only loads the top level nodes until you click on one then it loads the next level

     

    It should produce a much faster startup when you have a large drive but I haven't tested it as much as I would like.  Maybe you could test it for me and let me know if you find any problems.

     

    If you still need more explanation let me know and I will produce a commented version tomorrow.

     

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    GetFolders(Nothing, "c:\", TreeView1)

    End Sub

    Private Sub GetFolders(ByVal Node As TreeNode, ByVal Root As String, ByVal Tree As TreeView)

    If Node Is Nothing Then

    If Root.Length > 3 Then

    Node = Tree.Nodes.Add(Root, IO.Path.GetFileName(Root))

    Else

    Node = Tree.Nodes.Add(Root)

    End If

    End If

    Dim Folders() As String = IO.Directory.GetDirectories(Root)

    Array.Sort(Folders)

    For Each Folder As String In Folders

    Dim NewNode As TreeNode = Node.Nodes.Add(Folder, IO.Path.GetFileName(Folder))

    For Each SubFolder As String In IO.Directory.GetDirectories(Folder)

    NewNode.Nodes.Add("DummyFolder - We'll Never Show This")

    Exit For

    Next

    Next

    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

    If e.Node.Nodes(0).Text = "DummyFolder - We'll Never Show This" Then

    e.Node.Nodes.RemoveAt(0)

    GetFolders(e.Node, e.Node.Name, TreeView1)

    End If

    End Sub

    End Class

    Saturday, January 5, 2008 12:45 AM
  • Great! Like I've said. I've seen some crazy code 10x longer than this to do the same thing! Nice job Dave. Thanks for the source, make more since then the others that I've seen.... using "Friends" and so on...

    Thanks again
    FVxSF
    Saturday, January 5, 2008 1:53 AM
  •  Dave299 wrote:

    Hi FVxSF

     

    Thanks for that - although I don't think it's particularly original.

     

    I've come up with the following which doesn't use recursion, and only loads the top level nodes until you click on one then it loads the next level

     

    It should produce a much faster startup when you have a large drive but I haven't tested it as much as I would like.  Maybe you could test it for me and let me know if you find any problems.

     

    If you still need more explanation let me know and I will produce a commented version tomorrow.

     

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    GetFolders(Nothing, "c:\", TreeView1)

    End Sub

    Private Sub GetFolders(ByVal Node As TreeNode, ByVal Root As String, ByVal Tree As TreeView)

    If Node Is Nothing Then

    If Root.Length > 3 Then

    Node = Tree.Nodes.Add(Root, IO.Path.GetFileName(Root))

    Else

    Node = Tree.Nodes.Add(Root)

    End If

    End If

    Dim Folders() As String = IO.Directory.GetDirectories(Root)

    Array.Sort(Folders)

    For Each Folder As String In Folders

    Dim NewNode As TreeNode = Node.Nodes.Add(Folder, IO.Path.GetFileName(Folder))

    For Each SubFolder As String In IO.Directory.GetDirectories(Folder)

    NewNode.Nodes.Add("DummyFolder - We'll Never Show This")

    Exit For

    Next

    Next

    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

    If e.Node.Nodes(0).Text = "DummyFolder - We'll Never Show This" Then

    e.Node.Nodes.RemoveAt(0)

    GetFolders(e.Node, e.Node.Name, TreeView1)

    End If

    End Sub

    End Class

     

     

    Using the code above, which is great, when you look down the tree, and then find a folder that you want, how would then put the whole path into a string >?

     

    Sunday, April 27, 2008 8:45 AM
  •  

     

    This is what i currently have, but im sure there is a better way:

     

    Code Snippet
    savepath = "\\networkdrive\FILING\" & IO.Path.GetDirectoryName(TreeView1.SelectedNode.FullPath.ToString) & "\" & IO.Path.GetFileName(TreeView1.SelectedNode.ToString) & "\"

     

     

     

    Anyone ?
    Sunday, April 27, 2008 9:58 AM
  •  

     

    Found another way.  When the Array is sorted:

     

    Code Snippet
    Array.sort(folders)

     

     

     

    I put this after it

     

    Code Snippet

    Array.Sort(Folders)

    savepath = Folders(0) & "\"

     

     

    Sunday, April 27, 2008 10:31 AM
  •  Mr.Addy wrote:

     

     

    Found another way.  When the Array is sorted:

     

    Code Snippet
    Array.sort(folders)

     

     

     

    I put this after it

     

    Code Snippet

    Array.Sort(Folders)

    savepath = Folders(0) & "\"

     

     

     

     

    I take the above back... if you have more than one folder in it, it only picks the first one....  

     

    I Still need some help in understand this treeview control ???

    Sunday, April 27, 2008 12:18 PM
  • placesaver
    Sunday, June 21, 2009 5:41 PM