Home » Windows » How to count files in a folder with multiple subfolders in it

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?

Resolution

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

:recurse
setlocal
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%
(
     endlocal
     set /a fileCnt+=%fileCnt%
)
exit /b

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

Source: http://stackoverflow.com/questions/10702326/count-of-files-in-a-directory-with-count-of-files-in-sub-directories-also-report

Ned Sahin

Blogger for 20 years. Former Microsoft Engineer. Author of six books. I love creating helpful content and sharing with the world. Reach me out for any questions or feedback.

Leave a Comment