9

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)

nc4pk
  • 9,037
  • 14
  • 59
  • 71
ZEE
  • 918
  • 5
  • 19
  • 32
  • To the off-topic voters - questions about windows batch file programming are on-topic at [so] – DavidPostill Jul 18 '15 at 12:00
  • [batch file - counting number of files in folder and storing in a variable](http://stackoverflow.com/q/11004045/995714), [Batch file that counts the number of files in EVERY folder in a directory, and outputs results to a text file](http://stackoverflow.com/q/38775955/995714) – phuclv Nov 29 '16 at 09:44
  • Possible duplicate of [How can I check the size of a folder from the Windows command line?](https://superuser.com/questions/837016/how-can-i-check-the-size-of-a-folder-from-the-windows-command-line) – phuclv Mar 13 '19 at 02:23
  • @phuclv the question is about file count not file size :-) –  Apr 27 '19 at 15:30
  • @KassMonk maybe the one that I used to close this was wrong, but there are other duplicates I already mentioned – phuclv Apr 27 '19 at 15:37
  • Good point, sorry didnt see you were the same poster :-) –  Apr 27 '19 at 15:45

5 Answers5

7

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
  • 153,128
  • 77
  • 353
  • 394
3

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
  • 27,531
  • 11
  • 97
  • 118
  • 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 Jul 17 '15 at 23:54
  • @ZEE The `for` loop is not needed - see my answer. – DavidPostill Jul 18 '15 at 00:24
  • the `for` here is the superior solution to the creation of a temporary file in the other answer: the 'loop' is only executed once, for the single line of output of the command `dir *.xlsx /s /b 2^> nul ^| find "" /v /c`. But instead of writing that single line into a temp file, reading that file, and deleting the file, the `for`command directly reads the line. – HugoRune Aug 13 '23 at 06:54
2

A little late to the party, but I just wanted to show support for DavidPostill

DIR [LEAVE BLANK FOR ALL FILES, *.mp3, *.*] /B /A-D /S 2>NUL | FIND "" /V /C > tmp
SET /P COUNT=<tmp
SET /A COUNT -= 1
DEL tmp
ECHO !COUNT!

This is my implementation; I prefer to split things up (since the command creates a file, it helps to decrement by one).

NOTE: The above INCLUDES files that are HIDDEN or are SYSTEM files.
To exclude HIDDEN and SYSTEM files replace [DIR...] with this instead

DIR [LEAVE BLANK FOR ALL FILES, *.mp3, *.*] /B /A-D-S-H /S 2>NUL | FIND "" /V /C > tmp

Also, it should be noted that the [dir ...] method is, at least an order of magnitude, more efficient than the [for ... VAR+=1] method.
My case was 510,000 files; using the DIR method, ~6 SECs; using the FOR method, ~4 MINs.

JoeBro
  • 21
  • 2
2
set filesCount=0 & for %f in (*) do @(set /a filesCount+=1 > nul)
10100111001
  • 1,858
  • 1
  • 15
  • 11
1

You can take advantage of robocopy's /L (list) option. Then you don't need any complicated for loops or pipes. It's very fast too.

robocopy c:\mydir c:\dummy /L /E *.mp3 *.txt
  • c:\mydir: replace this with the path to the dir you want to search
  • c:\dummy: you can leave this, it is just a dummy arg that is ignored since we are using /L
  • /L: list only, will not copy/move anything.
  • /E: include subdirs recursively. You can remove this if you don't want to search subdirs.
  • If you don't want to print the list of files and dirs, you can add /NFL (No Files List) and/or /NDL (No Dirs List)

You will get a nice report like below. Just look at the Total column.

Source : c:\mydir

Files : *.mp3
        *.txt

--------------------------------------------------

{list of all the matching files}

--------------------------------------------------

            Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :         5         5         0         0         0         0
Files :        89        89         0         0         0         0
Bytes :   3.485 g   3.485 g         0         0         0         0
wisbucky
  • 2,928
  • 31
  • 29