travisowens Posted December 15, 2006 Posted December 15, 2006 (edited) 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. 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: 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. Edited December 15, 2006 by travisowens Quote Experience is something you don't get until just after the moment you needed it
Denaes Posted December 15, 2006 Posted December 15, 2006 (edited) 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. Edited December 15, 2006 by Denaes Quote
travisowens Posted December 15, 2006 Author Posted December 15, 2006 (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... 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. Edited December 15, 2006 by travisowens Quote Experience is something you don't get until just after the moment you needed it
mskeel Posted December 15, 2006 Posted December 15, 2006 (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. 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. Edited December 15, 2006 by mskeel Quote
Denaes Posted December 15, 2006 Posted December 15, 2006 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.