user2066241
user2066241

Reputation: 81

Batch files with variables containing parenthesis in nested if blocks

I have a .cmd file (Windows XP) that is using a multi-line IF block like this:

IF DEFINED SUMM (
    echo  %SUMM% >> %OutFile%
    echo  ---- >> %OutFile%
) ELSE (
    echo  No summary >> %OutFile%
)

The problem comes if the environmental variable %SUMM% contains a closing parenthesis and then text after it. For example:

SET SUMM=(Hello world) Here's some more text.

Then I presume the ')' in SUMM is treated as the closing parenthesis in the IF command block and generates an error:

Here's was unexpected at this time.

Is there any way around this? How can I ensure the ) is treated as part of the string, rather than part of the command block?

Upvotes: 0

Views: 275

Answers (1)

rojo
rojo

Reputation: 24466

Unless you can delay the expansion of SUMN, it's going to evaluate the contents of SUMN. So I don't think the syntax of if defined will allow what you want. Your best bet might be something like

if not "%SUMN%"==""

... if you know that %SUMN% won't contain quotation marks. If it could, then change the percents to exclamation marks and setlocal enabledelayedexpansion.

Upvotes: 1

Related Questions