Tuesday, August 27, 2013

Find Count of Files in a Folder at the Command Prompt

So you want to find a count of the number of files in a directory using the command prompt or a batch file, etc. Here is a really easy way to do it.

Just run the dir command and pipe the results to the find command, using the count parameter:

dir|find /c ".tif"
  

This will return a count of all the TIF files in your folder. You could replace ".tif" with whatever extension you want, such as ".pdf"


What about a folder with multiple types of files in it? Just add the /b parameter to the dir command to get the bare filenames, and then just look for the single period in the find command, like so:

dir /b|find /c "."


An alternate version that would count all the files in the directory and any sub directories (by adding the /s parameter to the dir command) would be:

dir /b /s|find /c "."


The above won't work if the filenames have other periods in them, like some.name.txt, but for the average filename, this should work just fine.


Lastly, here is the approach that will work with filenames, like some.name.txt, that have multiple periods in them, because it counts the lines, not the periods, in the output:

dir /b /a-d | find /c /v ""


That last one came from Joey:

Go Visit Joey's explanation...



Hope this helps someone out there...


Keywords: batch file, batch script counting files in a folder

2 comments:

  1. How to save the count in a batch variable?

    ReplyDelete
  2. It helped someone out there. My commnand propmtdays, like my autoexec.bat days, are long behind me. Needed this. Hope I never need to go back to edlin.

    ReplyDelete