Count files in a folder and subfolders from the command line

Count files in a folder and subfolders

Use the following command:

dir /b *.mp3 /s 2> nul | find "" /v /c > tmp && set /p count=<tmp && del tmp && echo %count%

The environment variable %count% will contain the number of files.

Note:

  • Remove /s if you don't want to count files in subfolders.

Example (using *.txt)

Directory listing to show the 17 files:

F:\test>dir /b *.txt /s
F:\test\abc.txt
F:\test\blackwhite.txt
F:\test\cpu.txt
F:\test\interface.txt
F:\test\Lorem ipsum.txt
F:\test\right.txt
F:\test\rights.txt
F:\test\software.txt
F:\test\tabs.txt
F:\test\test.txt
F:\test\this is inside junction.txt
F:\test\unique.txt
F:\test\xyz.txt
F:\test\sub\abc.txt
F:\test\sub\xyz.txt
F:\test\sub with space\junction sub with space.txt
F:\test\sub with space\xyz.txt

Run the command:

F:\test>dir /b *.txt /s 2> nul | find "" /v /c > tmp && set /p count=<tmp && del tmp && echo %count%
17

Further reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • find - Search for a text string in a file & display all the lines where it is found.

set filesCount=0 & for %f in (*) do @(set /a filesCount+=1 > nul)