Escaping dot in if-statement with echo in Windows batch

64 Views Asked by At

I have a chicken-and-egg issue: I need to echo a dot in an if statement in a batch script, but the command prompt crashes, saying '. was unexpected at this time.'

Note that echoing treats everything as literal, but the if statement interprets it as incorrect due to the dot being a special character.

Adding quotation marks solves the issue, but I don't want the batch user to see the error with quotation marks. I've tried using the caret ^ character to escape it, but it's not working.

Here's my script:

if "%found_7zip%"=="0" (
    echo 7-Zip or 7-Zip-ZStandard not found in Program Files or Program Files (x86).
    echo Please install 7-Zip or 7-Zip-ZStandard and try again.
    pause
    exit /b 1
)

Your help is appreciated! Thank you!

2

There are 2 best solutions below

1
jeb On BEST ANSWER

The solution is to escape the closing parenthesis.

echo 7-Zip or 7-Zip-ZStandard not found in Program Files or Program Files (x86^).

This is necessary, because the opening parenthesis in if "%found_7zip%"=="0" ( starts the scanning for the first closing parenthesis, independent of the position in a line.
It can escaped by a caret or by double quotes

2
kenny On

I found the answer in another SO question.

@echo|set /p="Please install 7-Zip or 7-Zip-ZStandard and try again."