2 Replies - 7759 Views - Last Post: 10 July 2014 - 12:19 AM Rate Topic: -----

#1 tangomouse  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 36
  • Joined: 22-October 09

list all folders on c: drive and get there attributes

Posted 13 November 2009 - 03:24 AM

how can i list every folder on c: drive ,i can only work out two levels "folder/subfolder" code below

Dim dir As New System.IO.DirectoryInfo("C:\")
Dim dirs As IO.DirectoryInfo() = dir.GetDirectories()
Dim d As DirectoryInfo
For Each d In dirs
ListBox1.Items.Add(d.Name + d.Attribute.ToString)
'get subfolder
Try
Dim subfd As IO.DirectoryInfo() = d.GetDirectories()
Dim nsub As DirectoryInfo
For Each nsub In subfd
ListBox1.Items.Add(nsub.Name + nsub.Attribute.ToString)
Next
Catch
End Try
Next

i could go on forever adding for and next loop for subfolder ,thay must be a easyer way..! thanks in advance

Is This A Good Question/Topic? 1
  • +

Replies To: list all folders on c: drive and get there attributes

#2 Sothrie  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 03-December 07

Re: list all folders on c: drive and get there attributes

Posted 13 November 2009 - 12:09 PM

This is a perfect example of a piece of code that could use a recursive function.

You're basically trying to do the same thing for each directory, so what you can do is create a function that takes in the directory as a parameter. The function would list the directory, and then run the function for all of the directories in that directory.

I make it sound confusing, I know. Here's a rough example of what I mean.

  'Start the function running from the program code
  Private Sub myProj_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	  LoadDirs("C:\")
  End Sub

  Private Sub LoadDirs(ByVal StartPath As String)
	  Dim dir As New System.IO.DirectoryInfo(StartPath)

	  'Print the current directory in our listbox
	  listbox1.Items.Add(d.Name + d.Attribute.ToString)

	  Dim dirs As IO.DirectoryInfo() = dir.GetDirectories()
	  Dim d As IO.DirectoryInfo

	  For Each d In dirs
			'Call the function for all of the subdirectories in the current directory
			LoadDirs(d.FullName)
	  Next
  End Sub



In the example above, the function starts at C, and then digs down into all of the directories below C.
So first it would run and print the C directory, then it will run and print the first directory under the C directory, then it will run again and print the first directory under the directory under C, and so on.

Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#3 craisin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-December 09

Re: list all folders on c: drive and get there attributes

Posted 10 July 2014 - 12:19 AM

You may find the following code (along the same lines) works (there is a slight bug in the earlier code supplied).
Iam using a TreeView object rather than a textbox
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim StartPath As String = "C:\"
        Dim dir As New System.IO.DirectoryInfo(StartPath)
        Dim dirs As IO.DirectoryInfo() = Dir.GetDirectories()
        Dim d As IO.DirectoryInfo
        For Each d In dirs
            'Call the function for all of the subdirectories in the current directory
            LoadDirs(d)
        Next
    End Sub
    Private Sub LoadDirs(ByVal d As IO.DirectoryInfo)
        'Print the current directory in our listbox
        Me.tvwTreeView1.Nodes.Add(d.FullName + " - " + d.Attributes.ToString)
    End Sub
End Class



I hope this helps someone :rolleyes2:/>/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1