JSI Tip 7885. How do I determine the size of a file, regardless of the version of CMD.EXE that is running?

In How to parse a batch parameter, I introduced the %~z1 syntax for determining the size of a file. I used this syntax in How can I determine the size of a file in a CMD script? and How can I determine the size and count of files in a folder from a command prompt or batch?

Some versions of CMD.EXE may not always properly return the file size. To workaround this behavior, I have scripted SizeFile.bat.

The syntax for using SizeFile.bat is:

call sizefile FileName size

Where FileName is the fully qualified path and file name of the file whose size you wish to determine, and size is a call directed numeric environment variable that will contain the size of the file.

SizeFile.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: SizeFile FileName Size&exit /b 1
if not exist %1 @echo SizeFile: %1 does NOT exist&exit /b 2
for /f "Tokens=3" %%a in ('dir %1 /A^|Findstr /i /c:"1 File(S)"') do (
 set p3=%%a
set p3=%p3:,=%
endlocal&set/a %2=%p3%
exit /b 0

Hide 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.