Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by travisowens
Experience is something you don't get until just after the moment you needed it
Posted (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 by Denaes
Posted (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 by travisowens
Experience is something you don't get until just after the moment you needed it
Posted (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 by mskeel
Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...