JSI Tip 7699. How can I count the number of files in a folder?


I have scripted FileCount.bat to count the number of files in a folder, and optionally include the count of files in all sub-folders.

The syntax for using FileCount.bat is:

\[call\] FileCount Folder Count \[/S\]

where Folder is the path to the folder whose files you wish to count, Count is a call directed numeric environment variable that will contain the count of files in Folder, and /S is an optional switch to include the count of files in all sub-folders.

FileCount.bat contains:

@echo off
setlocal
if \{%2\}

\{\} goto syntax set folder=%1# set folder=%folder:"=% set folder=%folder:\#=% set folder=%folder:#=% if not exist "%folder%\*.*" goto syntax set sub= if \{%3\}

\{\} goto top if /i \{%3\}

\{/s\} set sub= /s&goto top :syntax @echo Syntax: \[call\] FileCount Folder Count \[/S\] if \{%2\}

\{\} endlocal&exit /b 1 endlocal&set /a %2=0 exit /b 1 :top call :countit>nul 2>&1 endlocal&set /a %2=%count% exit /b 0 :countit set count=0 for /f "Tokens=1 Delims=\[\]" %%f in ('dir "%folder%\*.*" /b /as /ah /a-d%sub%^|find /n /v ""') do @set count=%%f



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish