Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check file size in Windows batch script

I want to check file size in a Windows batch script. It seems like my script is exiting instead of moving on to the next step in the script. The file condition should not generate an error and should move on to the next step. But I must be missing something or have something incorrect in the following:

:: Check size of input files 
call "%DIR_BAT%\CreateLAFMessage.bat" "%~n0.bat-> %STEP_NBR% - check for size of LOM_AA_2.dat and LOM_AI.dat" %_LAF_MSG_DETAILS%
set ERROR_MSG="%DIR_IP_INTERFACES%\LOM_AA_2.dat" ZERO Byte File
set file="%DIR_IP_INTERFACES%\LOM_AA_2.dat"
set maxbytesize=0
FOR /F "usebackq" %%A IN (%file%) DO set size=%%~zA
if %size% NEQ %maxbytesize% (
    echo.File is ^<> %maxbytesize% bytes
) ELSE (
    goto ON_ERROR
)
set ERROR_MSG="%DIR_IP_INTERFACES%\LOM_AI.dat" ZERO Byte File
set file="%DIR_IP_INTERFACES%\LOM_AI.dat"
FOR /F "usebackq" %%A IN (%file%) DO set size=%%~zA
if %size% NEQ %maxbytesize% (
    echo.File is ^<> %maxbytesize% bytes
) ELSE (
    goto ON_ERROR
)
like image 605
Burgesspatricia Avatar asked Aug 22 '26 15:08

Burgesspatricia


1 Answers

You have enabled the Usebackq option in the for loop. This option uses a different style of quoting:

  • Double quotes for long file names in "filenameset".
  • Single quotes for 'Text string to process'
  • Backquotes for `command to process`

Therefore, adding the double quotes tells the for loop to process the file.

@echo off
set "file=%DIR_IP_INTERFACES%\LOM_AA_2.dat"
set MAXBYTESIZE=0

for /f "usebackq" %%A in ("%file%") do set /a size=%%~zA

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

pause

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!