Windows 10

How to Check the Size of a File in a Windows Batch Script

In this tutorial, we are going to see how to check the size of a file in a Windows batch. Batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

How to Check the Size of a File in a Windows Batch Script

The following example finds the size of a file and if it is greater than MAXBYTESIZE displays “file is too large”:
 

 

@echo off

set "file=file1.txt"
set MAXBYTESIZE=500

FOR %%A IN (%file%) DO set size=%%~zA

if %size% GTR %MAXBYTESIZE% (
    echo %file% is too large
) else (
    echo %file% is %size% bytes long
)

Output:

file1.txt is too large

 

One thought on “How to Check the Size of a File in a Windows Batch Script

  • John Crosby

    How are you handling the error due to a zero-byte file using this technique?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *