JSI Tip 5725. How can I determine the size of a file in a CMD script?

In tip 0494, I explained how you parse a file name parameter.

In tip 5535, I explained how a called script can return a call directed environment variable.

Using these techniques, I have scripted FileSize.bat, to return the size of a file, in bytes.

The syntax for using FileSize.bat is:

Filesize Filename FileSizeVariable


Filename is the file whose size you want returned.

FileSizeVariable is the name of the arithmetic environment variable that will contain the file size, in bytes. If the FileSizeVariable variable does NOT exist, FileSize.bat will create it.

FileSize.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: Filesize Filename FileSizeVariable&goto :EOF
if not exist %1 @echo Syntax: Filename must exist&goto :EOF
set /a %2=%~z1

Sample Usage

To determine the size of the current users NTUser.dat file:

call FileSize "%UserProfile%\ntuser.dat" HKCU_RegSize
@echo The size of %username%'s ntuser.dat file is %HKCU_RegSize% bytes.

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.