0

Below is a BAT file sample which shows a curious behaviour. When a "SET" is used inside () an IF statement we get an error, if we have other code besides a SET then no problem.

I created a work around by doing the SET on the IF line and doing the other logic I need in a normal
IF ... ( stuff ) ELSE ( more )

Does someone know what is going on here?

NB: Same outcome if SETLOCAL is used

::SETLOCAL ENABLEDELAYEDEXPANSION
echo **This works**
@set InstallerTest=false
@IF "%InstallerTest%"=="true" @SET SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signtrue.exe
@IF NOT "%InstallerTest%"=="true" @SET SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signfalse.exe
SET SESign_Exe

ECHO **THIS PRODUCES NO ERROR** 
@IF "%InstallerTest%"=="true" (
    echo IN TRUE PART
) ELSE (
    ECHO IN FALSE PART
)

echo **this produces : \Schneider was unexpected at this time.**
@IF "%InstallerTest%"=="true" (
    SET SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signtrue.exe
) ELSE (
    SET SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signfalse.exe
)
::ENDLOCAL
Mofi
  • 38,783
  • 14
  • 62
  • 115
Greg B Roberts
  • 161
  • 2
  • 12
  • `SET "SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signtrue.exe"` – user4003407 Dec 11 '15 at 00:04
  • Yes this works! thanks, any idea why of is this just a quirk in the BATCH handling ? – Greg B Roberts Dec 11 '15 at 02:22
  • 4
    You are in parenthesis context. In that context an un-escaped, un-quoted `)` have a special meaning of closing parenthesis context. And value of `ProgramFiles(x86)` environment variable have an `)` in it. – user4003407 Dec 11 '15 at 02:28
  • Thanks, makes sense now that it was the data in the string, not the syntax. Don't seem to be able to flag as answer. – Greg B Roberts Dec 11 '15 at 02:39
  • 2
    @GregBRoberts Never hurts to use parentheses around all your SET commands. I try to make a habit of it. `set "var=cat and dog"` – Squashman Dec 11 '15 at 03:21

1 Answers1

1

Open a command prompt window and execute there cmd /?. At bottom of the last help page output into the console window you can read which characters in strings require surrounding double quotes: the space character and &()[]{}^=;!'+,`~

The reason is easy to understand. The space character is the separator for the "words" (commands, options, parameters, keywords, ...) on command line.

The other characters have special meanings in Windows command line syntax. A not escaped and not in quoted string defined ( marks beginning of a block an ) marks end of a block if not escaped or within a quoted string.

Therefore you need

set "SESign_Exe=%ProgramFiles(x86)%\Schneider Electric\CodeSign\signtrue.exe"

And you should really use always set "variable=value".

See for example answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation with an example why using set "variable=value" is always better than using just set variable=value.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115