Brans Ds
Brans Ds

Reputation: 4117

escape character in IF

i tried this in different variations (like setlocal EnableDelayedExpansion)- but nothing works:

echo off
IF "%1"=="Debug^|Win32"  set servicesConfig=Win2008.Debug
echo Incorrect parametr %servicesConfig%
pause > nul

Upvotes: 1

Views: 72

Answers (2)

Endoro
Endoro

Reputation: 37569

if you make the following call: script.bat "Debug|Win32" this works:

@echo off&setlocal
IF "%~1"=="Debug|Win32" set "servicesConfig=Win2008.Debug"
echo Incorrect parametr %servicesConfig%

output is:

Incorrect parametr Win2008.Debug

Upvotes: 1

jeb
jeb

Reputation: 82277

As quotes escapes special characters you compare the content of %1 with Debug^|Win32 (including the caret).

In your case you should use this

@echo off
IF "%~1"=="Debug|Win32" (
  echo It's ok
  set servicesConfig=Win2008.Debug
) ELSE (
  echo Unknown in "%~1"
)

You can call it with

test.bat "Debug|Win32"
or
test.bat Debug^|Win32

The "%~1" will enclose the first parameter in quotes, so special charaters are harmless, but if the first parameter is already quoted the %~ will strip them before adding the new quotes, so you always get only one enclosing.

Upvotes: 1

Related Questions