Running this batch file
@echo off
set a=some value with (parentheses) inside
if 1 == 1 (
set PATH=%a%
)
gives inside was unexpected at this time. error.
How to escape a variable to avoid this error?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use two different ways
Use the extended syntax of
setwith quotesset "var=content"will set var with content,content is quoted so special characters aren’t problematic and it uses the content till the last quote (without the quote itself)
Use delayed expansion (like the answer of shf301) but also transfer the value to the main scope.
In this case the extended set-syntax is not necessary, I used it only to avoid hidden spaces at the line end.
EDIT:
Can I combine this with setlocal EnableDelayedExpansion and using ! instead of % to lazy evaluate variable’s value? When I tried I got )! was unexpected at this time.
You can, but it’s contra productive, as
Then your path contains carets in front of the
)likeC:\programs (x86^)To understand how expansion works you can read SO:How does the Windows Command Interpreter (CMD.EXE) parse scripts?
EDIT2: More problems with the path (containing quotes)
According to this question there can occour another problem with parenthesis when the path contains quotes.
Sample
path="C:\Program Files (x86)";C:\Program Files (x86)\SkypeThis is allowed, even it’s not necessary to use quotes here, but this destroys the extended
SETsyntax, as nowset "newPath=%path%"expands toNow at least one parenthesis is not inside quotes and is able to break a command block.
But you can simply remove all quotes from the path variable, as said, quotes aren’t necessary here.