Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need a bit of help counting folders and subfolders while my program iterates through the filesystem.

I have seen many examples of how to count files inside of folders but I only wish to add +1 to a count for each new directory the iterator travels through.

I hope my question is clear enough and that someone can help me

Ok this is the code I am using thus far but it returns all directory and sub directory counts all at once and not one by one.


Public Function CountDirectories(ByVal dir As DirectoryInfo) As Integer
            Try
                Dim MySubdirs() As DirectoryInfo = dir.GetDirectories()
                Dim count As Integer = MySubdirs.Length
                For Each subDir As DirectoryInfo In MySubdirs
                    count += CountDirectories(subDir)
                Next subDir
                Return count
            Catch ex As Exception
            End Try
        End Function  



Then I am able to show that count to a label by using:


Dim MyDirectoryInfo As New DirectoryInfo(pathToSearchCombo.Text)
           Dim Mytotalcount As Integer = CountDirectories(MyDirectoryInfo)
           Label12.Text = Mytotalcount  


I am looking for a method that will return a folder count one by one while my program iterates the filesystem.

thank you and sorry if this is simple I just cannot think of how??
thank you
cheers!
Posted
Updated 11-Aug-12 17:50pm
v2
Comments
[no name] 11-Aug-12 22:11pm    
How is this different that your other question about counting directories?
Tarun Mangukiya 11-Aug-12 22:40pm    
Sorry,
But as I think you know how to count files in any folder. Then What's the problem? Just add 1 as your wish to the output count...
Dale 2012 11-Aug-12 23:16pm    
I am aware of how to count files as they iterate but I wish to count the folders that contain the files to a different string or label as it iterates. Can you give a link or example of how this is done?

thank you in advance

1 solution

This is done the exact same way as counting the files in a folder. You just get the directories in the folder instead of the files.

Without seeing the code you're using to count the files, it's difficult to tell you exactly what to change.
 
Share this answer
 
Comments
Dale 2012 11-Aug-12 23:44pm    
I have posted some code that I am using. will you assist me with this issue please?

thank you very much!
Dave Kreskowiak 11-Aug-12 23:53pm    
OK, now you've completely changed the question.

You need to move the CountDirectories code to a background thread and have it periodically update the status every time you are around to return from a recursive call.

Google for and read the documentation on "BackgroundWorker". It makes this job pretty easy.
Dale 2012 12-Aug-12 2:21am    
I have done exactly what you have said by placing the count directories within a background worker and then calling it from my iteration but again it returns a one lump sum of how many directories instead of counting normally one folder by one folder.
Dave Kreskowiak 12-Aug-12 11:43am    
Again, without seeing the code it's ptretty much impossible to tell you where you went wrong.
Dale 2012 12-Aug-12 19:09pm    
Public Function CountDirectories(ByVal dir As DirectoryInfo) As Integer
Try
Dim MySubdirs() As DirectoryInfo = dir.GetDirectories()
Dim count As Integer = MySubdirs.Length
For Each subDir As DirectoryInfo In MySubdirs
count += CountDirectories(subDir)
Next subDir
Return count
Catch ex As Exception
End Try
End Function


Dim MyDirectoryInfo As New DirectoryInfo(pathToSearchCombo.Text)
Dim Mytotalcount As Integer = CountDirectories(MyDirectoryInfo)
Label12.Text = Mytotalcount

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900