0

Heres a simplified version of what I'm after:

SET outFile=
IF "%outFile%"=="" (
    echo Output file=[Console]
    SET outFile=CON
) ELSE ( 
    IF "%outFile:~0,3%"=="%HOMEDRIVE%\" (
        echo Output file=%outFile%
        SET outFile=%outFile%
    ) ELSE ( 
        echo Output file=%cd%\%outFile%
        SET outFile=%cd%\%outFile%  
    )
)

It works in all conditions except for outFile being empty which returns the error "( was unexpected at this time."

Nathan
  • 435
  • 4
  • 16

1 Answers1

0

Add setlocal enabledelayedexpansion in your batch, and use !outFile:~0,3! inside your second IF, like below:

setlocal enabledelayedexpansion

IF "%outFile%"=="" (
    echo Output file=[Console]
    SET outFile=CON
) ELSE ( 
    IF "!outFile:~0,3!"=="%HOMEDRIVE%\" (
        echo Output file=%outFile%
        SET outFile=%outFile%
    ) ELSE ( 
        echo Output file=%cd%\%outFile%
        SET outFile=%cd%\%outFile%  
    )
)

Furthermore, you could take a look at this thread to get more info about What is the proper way to test if variable is empty in a batch file?

Community
  • 1
  • 1
Rubik
  • 1,431
  • 1
  • 18
  • 24