1

I'm having an issue with the batch script I wrote below. If I take out the "if" statement, it works, but doesn't print out the %size%, if I leave the "if" statement it gives me an error about "0 was unexpected at this time."

I really don't see any syntax errors here, and if I leave echo on, I see the variables getting set with the proper values. Ultimately I want this to restore files if it detects they're in a bad state, but I'm a little confused as to why the variables don't seem to be working properly.

    @echo off
    set folder="C:/Somedir/"
    set backupfolder="C:/Backupdir/"
    set minbytesize=0

    for /R "%folder%" %%I in (*) do (
        set size=%%~zI 
        set file=%%~nxI

        echo %file% is %size%

        if %size% EQU %minbytesize% (
              REM do something
        )
    )

    pause
  • Possible duplicate of [Variables in batch not behaving as expected](https://stackoverflow.com/questions/30282784/variables-in-batch-not-behaving-as-expected) – Squashman Aug 02 '18 at 22:13
  • There is no need to assign the `FOR` meta-variables to environmental variables within your code. Just use the `FOR` meta-variables directly. – Squashman Aug 02 '18 at 22:15

1 Answers1

0

the old delayed expansion pitfall:

@echo off
setlocal enableDelayedExpansion
set "folder=C:/Somedir/"
set "backupfolder=C:/Backupdir/"
set "minbytesize=0"

for /R "%folder%" %%I in (*) do (
    set "size=%%~zI" 
    set "file=%%~nxI"

    echo !file! is !size!

    if !size! EQU !minbytesize! (
          REM do something
    )
)

pause
endlocal

more you can find here: ss64.com/nt/delayedexpansion.html

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thanks, this was definitely part of it! I also had to use /i for the for loop and /a for setting the variable. Thanks! – user3763099 Aug 02 '18 at 21:29