0

I am trying to check the size of a file using a batch file 'run.bat'. The batch script is-

setlocal
set file="C:\TestWorks\Project_Testing\new.template"
set maxbytesize=2000

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

if %size% LSS %maxbytesize% (
echo File is ^< %maxbytesize% bytes
) else (
echo File is ^>= %maxbytesize% bytes

When I am running this it is not giving proper output, I am getting the output like this-

C:\TestWorks\Project_Testing>run.bat

C:\TestWorks\Project_Testing>set file="C:\TestWorks\Project_Testing\new.template

C:\TestWorks\Project_Testing>set maxbytesize=2000

C:\TestWorks\Project_Testing>FOR /F "usebackq" %A IN ('"C:\TestWorks\Project_Testing\bootstrap.bat.template"') DO set size=%~zA

C:\TestWorks\Project_Testing>set size=794
C:\TestWorks\Project_Testing>C:\TestWorks\Project_Testing>

What is going wrong here.

Som Sarkar
  • 289
  • 1
  • 5
  • 24

2 Answers2

0

While most of the code works, there are some minor problems that can lead to bigger problems in other cases.

@echo off
    setlocal enableextensions

    set "file=C:\TestWorks\Project_Testing\new.template"
    if not exist "%file%" goto :eof

    set "maxbytesize=2000"

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

    if %size% LSS %maxbytesize% (
        echo File is ^< %maxbytesize% bytes
    ) else (
        echo File is ^>= %maxbytesize% bytes
    )

Changes:

  • Proper quoting. While this is not the reason for the failure, it is better to properly quote the data

  • for /f is intended to process lines of data. To directly get references to files, a simple for should be used (not, still not the reason for the failure).

  • And, the reason for your code malfunction is the missing parenthesis in the else clause (see here)

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Heh, heh, heh...

You're missing the closing parenthesis of the if statement.

Append a single ) on al line all by its lonesome after that last echo and maybe the batch-gods will smile upon you.

Magoo
  • 77,302
  • 8
  • 62
  • 84