1

I have the following batch script and i keep getting the error "0 was unexpected at this time."

IF "%Logs%"=="true" (
    for /f %%i in ('dir \%Device%\logs') do set cmd=%%i
        if %cmd% GTR 0 (
            echo Folder does exist.
        )
)

I think I have to fix it with delayed expansion but I don't know how, I would appreciate any help, thanks in advance!

Jan Tamm
  • 117
  • 2
  • 11

1 Answers1

1

You have to enable delayed expansion with setlocal enabledelayedexpansion and then use it with !var! instead of %var%:

@echo off
setlocal enabledelayedexpansion
IF "%Logs%"=="true" (
    for /f %%i in ('dir /ad /b "\%Device%\logs" ^|find /c /v ""') do set cmd=%%i
    if !cmd! GTR 0 (
       echo Folder does exist.
    )
)

Note: for an empty folder you will get a value of 2 (because of . and ..). Either adapt to ... GTR 2 or use another approach (like above).

Stephan
  • 53,940
  • 10
  • 58
  • 91