Count the number of files in a directory (Windows)

Was randomly asked to find out the number of files in the project at work and most searches on Google turned up instructions for doing it in Unix/Linux. Figured I should put the Windows version here for future reference.

Fire up MS-Command Prompt and...

dir "c:\myworkspace" /s /b | find /c ".txt"
Returns the count of number of text files in current directory (myworkspace) and all sub-directories.

dir "c:\myworkspace" /s /b | find ".txt" > listOfFiles.txt
Outputs the list of text files in current directory and all sub-directories to the file listOfFiles.txt


How it works:
Taken from Computer Hope.com - MS DOS and command prompt

dir [path] [attributes]
Displays a list of files and subdirectories in the directory specified by [path].
/sDisplays files in specified directory and all subdirectories.
/bUses bare format (no heading information or summary).


find [attribute] "string"
Searches for a text string in a file or files.
/cDisplays only the count of lines containing the string.



Thus by piping the output of the dir command to find, we count the number of times ".txt" appears in the output of the dir command instead of the number of times ".txt" appears in the files within the directory.

Happy counting! ^^

Comments

Popular posts from this blog

Exclude directories from chown

Flash game: Cargo Bridge

Copying Sony recovery partition to new harddisk