Skip navigation

JSI Tip 7739. How can I determine the size and count of files in a folder from a command prompt or batch?

I have scripted SizeCnt.bat to determine the size and count of files in a folder, and optionally in all sub-folders.

The syntax for using SizeCnt.bat is:

SizeCnt Folder \[/S\]

where Folder is the path to the folder you wish to measure, and /S is an optional switch that will also include the size and count of the files in all of Folder's sub-folders.

The output is displayed on the CMD console, but you can pipe it to a file using the following syntax:

SizeCnt Folder \[/S\]>FileName

You can import the size and count into environment variables in your script, as in:

for /f "Tokens=1,2" %%s in ('SizeCnt Folder \[/S\]') do set /a FldSize=%%s&set /a fileCnt=%%t

SizeCnt.bat contains:

@echo off
setlocal
If \{%1\}

\{\} goto Syntax set folder=%1# set folder=%folder:"=% set folder=%folder:\#=% set folder=%folder:#=% if not exist "%folder%\*.*" goto Syntax if \{%2\}

\{\} goto begin set xx= %2 set xx=%xx:"=% if /i "%xx%" NEQ " /s" goto Syntax :begin set /a cnt=0 set /a tot=0 pushd "%folder%" call :doit>nul 2>&1 popd @echo %tot% %cnt% endlocal exit /b 0 :doit for /f "Tokens=*" %%s in ('dir /b /as /ah /a-d%xx%') do call :addsize "%%s" goto :EOF :Syntax @echo Syntax: SizeCnt Folder \[/S\] endlocal exit /b 1 :addsize set /a sz=%~Z1 set /a tot=%tot% + %sz% set /a cnt=%cnt% + 1



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