Counting all the files in a directory tree

travisowens

Centurion
Joined
Feb 11, 2004
Messages
108
Location
Rochester, NY
Counting all the files in a directory tree (resolved)

THIS PROBLEM IS RESOLVED

I simply want to count all the files (of a certain file extension) in a folder & all of it's subfolders. I came across this code on another forum but it only returns 0.

C#:
return System.IO.Directory.GetFiles( sFolder , sExtension ).Length;
So I changed changed it as it seems the poster forgot one of the requirements was to support subfolders so I changed it like so:

C#:
return System.IO.Directory.GetFiles( sFolder , sExtension , SearchOption.AllDirectories ).Length;
Any ideas? Even when the extension is "*.*" it fails, and this folder tree has 14,000+ files, some in the root folder and tons in many sub folders.
 
Last edited:
Visual Basic:
IO.Directory.GetFiles("C:\\New Folder", "*.BMP", IO.SearchOption.AllDirectories).Length

IO.Directory.GetFiles("C:\\New Folder", "*", IO.SearchOption.AllDirectories).Length

The top Line returned the number of bitmaps I had in sub directories of New Folder and the second line returned all the files, so this format seems to work.

Though I did try with only a few hundred, not tens of thousands, there might be a problem with such large amounts of files.
 
Last edited:
I have no idea what was wrong with the code, but I recreated my method from scratch and it worked, here's the code...

C#:
public int GetFileCount(string sFolder, string sExtension)
{
	return System.IO.Directory.GetFiles( sFolder , sExtension , SearchOption.AllDirectories ).Length;
}
The reason I'm executing this is to set a Max value on a progress bar (as each file I analyze later in the code increments the progress bar by 1). When I originally used a recursive function it took about 3 secs just to calculate the total count, but using this much slicker solution it happens near instantly.

So if you plan on doing something with the filters (ex: reading their contents, checking file meta data, etc) you want recursion, but if you merely want a file count, this is way more efficient.
 
Last edited:
I've always rolled my own recursive function to do this and never really took the time to research it further. Not the best solution, I know, so I'd be happy to learn of a one liner .Net way to do it.

C#:
        private int GetCountOfFiles(string path)
        {
            int count = Directory.GetFiles(path).Length;

            foreach (string dir in Directory.GetDirectories(path))
            {
                count += GetCountOfFiles(dir);
            }

            return count;
        }

Edit: Sorry...I hit to reply, hunted out the code, and by the time I posted, the thread was resolved. Nice solution! I will be using that in the future and do away with my hand written mess.
 
Last edited:
mskeel said:
I've always rolled my own recursive function to do this and never really took the time to research it further. Not the best solution, I know, so I'd be happy to learn of a one liner .Net way to do it.

<snip code>

Edit: Sorry...I hit to reply, hunted out the code, and by the time I posted, the thread was resolved. Nice solution! I will be using that in the future and do away with my hand written mess.

Same here. In fact when I first saw this thread my first thought was a recursive function. Still I often have to do things with the files (open them, count the size, lines, move/copy, etc) so I can't just trash my Recursive Files in Directory Snippit
 
Back
Top