Search code examples
windowsbatch-filefilesize

Checking file size in a batch script


I am trying to find the size of a file and if it is greater than 0, I want to do some stuff. I have this code:

set file="C:\AnalyzerCheck\loaded.txt"
set minbytesize=0
if exist %file% (
FOR /F "usebackq" %A IN ('%file%') DO set size=%~zA
if %size% GTR %minbytesize% (
    //do stuff
) else (
    //do stuff
)

However, I am getting this ouput/error when I run the script:

C:\AnalyzerCheck>set file=C:\AnalyzerCheck\loaded.txt

C:\AnalyzerCheck>set minbytesize=0

file~zA was unexpected at this time.

C:\AnalyzerCheck>FOR /F "usebackq" file~zA

C:\AnalyzerCheck>

How do I fix this error?

Edit:

New error:


Solution

  • This command:

    FOR /F "usebackq" %A IN ('%file%') DO set size=%~zA
    

    have two errors: You must not use /F option (neither "useback" option) because you want not to read the file CONTENTS, but just process the file NAME. Also, if this command is inside a Batch file, the A replaceable parameter must have two percent signs:

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