Skip to main content

How to Get a File's Size in Bytes in Batch Script

Knowing the size of a file is a common requirement for many scripting tasks, such as verifying a download, checking for empty files before processing, or logging the size of backups. Windows Batch provides a simple and direct way to retrieve a file's size in bytes without needing external tools, using a special parameter expansion within a FOR loop.

This guide will show you how to use the %%~zF modifier to get a file's size, store it in a variable for later use, and apply this technique in a practical script that logs the sizes of multiple files.

The Core Method: Using FOR Command Expansion

The standard way to get a file's size is with the FOR command, which is typically used for looping. When used with a single file, it iterates just once, but it gives us access to powerful metadata about that file.

The key is the ~z modifier applied to the loop variable.

  • FOR %%F IN (filename.txt) DO ... : Sets up a loop where %%F will hold the filename.
  • %%~zF: Inside the loop, this special syntax expands %%F not to the filename, but to the size of the file in bytes.

Basic Example: Echoing the File Size

Let's start with a simple script that directly prints the size of a known file.

Assume we have a file named report.docx.

@ECHO OFF
REM This script will display the size of the file "report.docx".

FOR %%I IN ("report.docx") DO (
ECHO The file size of "%%I" is %%~zI bytes.
)
note

The filename is in quotes to handle potential spaces.

Output (Assuming report.docx is 24,576 bytes):

The file size of "report.docx" is 24576 bytes.

Storing the File Size in a Variable

More often than not, you'll want to capture the file size in a variable to use it in other parts of your script, for example, in an IF statement.

This script retrieves the file size and stores it in a variable named FILESIZE.

@ECHO OFF
SET "FILENAME=report.docx"
SET "FILESIZE="

REM Use the FOR loop to get the size and SET the variable.
FOR %%I IN ("%FILENAME%") DO SET "FILESIZE=%%~zI"

REM Check if the variable was set before using it.
IF DEFINED FILESIZE (
ECHO The file "%FILENAME%" is %FILESIZE% bytes.
) ELSE (
ECHO Could not determine the size of "%FILENAME%". It may not exist.
)

Output:

The file "report.docx" is 24576 bytes.
note

This is the most common and useful pattern for retrieving a file's size for script logic.

Common Pitfalls and How to Solve Them

While the method is reliable, you must handle cases where the file doesn't exist or is passed as a parameter.

Problem: Handling "File Not Found" Errors

If the file specified in the FOR loop does not exist, the loop body will not execute. This isn't an error that crashes the script, but it means your variable will never be set.

Let's see the error:

@ECHO OFF
SET "FILENAME=non_existent_file.txt"
SET "FILESIZE="

FOR %%I IN ("%FILENAME%") DO SET "FILESIZE=%%~zI"

REM This will produce incorrect output because FILESIZE was never set.
ECHO File size is: %FILESIZE%

Output (the variable is empty, which can be misleading)

File size is:

Solution: Check with IF EXIST First

The best practice is to always verify the file exists before attempting to get its size.

@ECHO OFF
SET "FILENAME=non_existent_file.txt"

IF NOT EXIST "%FILENAME%" (
ECHO Error: File not found.
GOTO :EOF
)

FOR %%I IN ("%FILENAME%") DO SET "FILESIZE=%%~zI"
ECHO File size is: %FILESIZE% bytes.

Output (a clear, correct error message)

Error: File not found.

Problem: Working with a Filename Passed as an Argument

This technique works perfectly with command-line arguments (%1, %2, etc.), which is useful for creating reusable tool scripts.

Save this as GetSize.bat. It will report the size of whatever file you pass to it.

@ECHO OFF
REM This script gets the size of the file passed as the first argument.

SET "TARGET_FILE=%~1"

IF NOT EXIST "%TARGET_FILE%" (
ECHO Error: File "%TARGET_FILE%" not found.
GOTO :EOF
)

FOR %%F IN ("%TARGET_FILE%") DO (
ECHO Size of "%%~nxF" is %%~zF bytes.
)
note

Note the use of %~1 to handle quoted paths and %%~nxF to display just the name and extension of the file.

Usage and Output:

C:\> GetSize.bat "C:\Windows\System32\kernel32.dll"
Size of "kernel32.dll" is 786432 bytes.

Practical Example: Logging the Size of All ZIP Files

You can combine this technique with wildcards (*) to perform actions on multiple files. This script finds all .zip files in the current directory and creates a CSV report with their names and sizes.

@ECHO OFF
SETLOCAL
SET "REPORT_FILE=size_report.csv"

ECHO Generating file size report...

REM Create the CSV header (overwrite existing file).
(ECHO "Filename","Size in Bytes") > "%REPORT_FILE%"

REM Loop through all .zip files and append their info to the CSV.
FOR %%F IN (*.zip) DO (
ECHO "%%~nxF","%%~zF" >> "%REPORT_FILE%"
)

ECHO Report generated: %REPORT_FILE%
ENDLOCAL

And, resulting size_report.csv created file (Assuming you have archive1.zip and project_backup.zip in the folder):

size_report.csv
"Filename","Size in Bytes"
"archive1.zip","153600"
"project_backup.zip","2867200"

Conclusion

The %%~zF parameter expansion within a FOR loop is the standard, built-in, and most reliable method for getting a file's size in a Windows Batch script. It is efficient, accurate, and easy to integrate into your scripts for validation, reporting, or conditional logic.

By storing the result in a variable and always preceding the check with an IF EXIST statement, you can build robust scripts that handle file operations safely and predictably.