How to count files in a folder with multiple subfolders in it

Imagine you have a folder that consists of hundreds of subfolders which have tousands of files in them. What would you do if you need to count have files you have in total?


Simply create a text file and add the script below in. Change file format as “bat“. Run it in Command Prompt (C:>.\countfiles.bat)

@echo off
setlocal disableDelayedExpansion
if "%~1"=="" (call :recurse ".") else call :recurse %1
exit /b

set fileCnt=0
for /d %%D in ("%~1\*") do call :recurse "%%~fD"
for /f %%A in ('dir /b /a-d "%~1\*" 2^>nul ^| find /v /c ""') do set /a fileCnt+=%%A
echo "%~f1" %fileCnt%
     set /a fileCnt+=%fileCnt%
exit /b

It will list directories with file counts. You can calculate the total by using simple Excel functions.


Leave a Reply