Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File count from directories

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

I have the following code that will list all directories and sub directories
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim DirList As New ArrayList
        GetDirectories("C:\Tools", DirList)
        
    End Sub

    Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
        Dim Dirs() As String = Directory.GetDirectories(StartPath)
        DirectoryList.AddRange(Dirs)
        For Each Dir As String In Dirs
            GetDirectories(Dir, DirectoryList)
            ListBox2.Items.Add(Path.GetDirectoryName(Dir))
        Next
    End Sub
What iam trying to achive is to get the file count for each directory or sub directory.

Example of how the listbox should look

c:\tools = 0
c:\tools\test1 = 23
etc...

Any help would be appreciated
Thanks
 
Like so:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim DirList As New ArrayList
GetDirectories("C:\Tools", DirList)

End Sub

Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
Dim Dirs() As String = Directory.GetDirectories(StartPath)
DirectoryList.AddRange(Dirs)
For Each Dir As String In Dirs
GetDirectories(Dir, DirectoryList)

[red]Dim f() As String = Directory.GetFiles(StartPath)[/red]

ListBox2.Items.Add(Path.GetDirectoryName(Dir)[red] & " = " & UBound(f) + 1[/red])
Next
End Sub



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 

hugh999, I am new to VB.NET and I try to use the pieces of code I find in this group to learn this language.
When I have your code, I have a couple of "Name 'Directory' (and Path) is not declared" messages.

Do I need:
Code:
[blue]Imports[/blue] [red]Something.Or.The.Other[/red]
at the top of my (your) code in order to run it?

Have fun.

---- Andy
 
Andrzejek, you need:

[tt]Imports System.IO[/tt]


or you could use:

[tt]System.IO.Directory.GetDirectories(StartPath)[/tt]

and not use the Imports directive.


Hope this helps.

[vampire][bat]
 

Thank you, earthandfire. It works now.
Kind of.... Some directories are mentioned several times with the same number of files, but I guess I need to work it out for myself.


Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top