Count files in a folder and subfolders from the command line

6

1

How do I count all files of a given type (eg. *.mp3) in a designated folder (and optionally subfolders) from command line into a environment variable?

(no PowerShell please, just batch commands)

ZEE

Posted 2015-07-17T23:33:26.100

Reputation: 571

To the off-topic voters - questions about windows batch file programming are on-topic at [so] – DavidPostill – 2015-07-18T12:00:21.470

Possible duplicate of How can I check the size of a folder from the Windows command line?

– phuclv – 2019-03-13T02:23:35.733

@phuclv the question is about file count not file size :-) – Under A Tree – 2019-04-27T15:30:30.083

@KassMonk maybe the one that I used to close this was wrong, but there are other duplicates I already mentioned – phuclv – 2019-04-27T15:37:46.913

Good point, sorry didnt see you were the same poster :-) – Under A Tree – 2019-04-27T15:45:01.350

Answers

2

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

10100111001

Posted 2015-07-17T23:33:26.100

Reputation: 1 664

Hmm. This solution gives 13 files for my test case. I wonder where the missing 4 files have gone? – DavidPostill – 2015-07-18T00:29:27.490

Are those 4 files in a subfolder? – 10100111001 – 2015-07-18T00:37:07.240

Ah. Yes, that's where they are hiding ;) – DavidPostill – 2015-07-18T00:38:45.477

I think this is an elegant solution... accepted! – ZEE – 2015-07-18T01:31:59.357

5

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

DavidPostill

Posted 2015-07-17T23:33:26.100

Reputation: 118 938

1You may consider adding 2> nul to your dir command to omit the error message if no files are found. – Steven – 2015-07-18T00:28:32.207

@Steven Thx. Done – DavidPostill – 2015-07-18T00:33:19.633

1

Use a combination of dir and find to count the files. Store the files into a variable via the for loop. Redirect error output to nul to hide File Not Found error.

@echo off
for /f %%i in ('dir *.xlsx /s /b 2^> nul ^| find "" /v /c') do set VAR=%%i
echo %VAR%

See descriptions of parameters using /? for dir, find, and for.

Steven

Posted 2015-07-17T23:33:26.100

Reputation: 24 804

Thanks... its a solution... but i wonder if we can simplify that... seems a bit of overload to pass all that info to a filter... even more if there are thousands of files in the folders – ZEE – 2015-07-17T23:54:16.683

@ZEE The for loop is not needed - see my answer. – DavidPostill – 2015-07-18T00:24:22.280