3

I cannot tell why the context of an if would change my ability to run the same code.

While this:

@SET FILE_MAIL_BODY=mail.body
@echo The build FAILED because of a lock on the 'classes' folder (or its subfolders).>%FILE_MAIL_BODY%

works just fine, running the following snippet:

@SET retval=%ERRORLEVEL%
if %retval% == 1 (
    @SET FILE_MAIL_BODY=mail.body
    @echo The build FAILED because of a lock on the 'classes' folder (or its subfolders).>%FILE_MAIL_BODY%  
)

fails with the message:

. was unexpected at this time.

, unless I delete the . after subfolders.

2

1 Answer 1

3

You have to escape the () open and closed parenthesis characters in the ECHO command with the ^ caret character (see below) so it'll be: @echo The build FAILED because of a lock on the 'classes' folder ^(or its subfolders^).>%FILE_MAIL_BODY%.

@SET retval=%ERRORLEVEL%
if %retval% == 1 (
    @SET FILE_MAIL_BODY=mail.body
    @echo The build FAILED because of a lock on the 'classes' folder ^(or its subfolders^)^.>%FILE_MAIL_BODY%  
)

Further Resources

  • Some characters characters, have special meaning to the command line. They cannot be printed as text using the ECHO command unless escaped using the caret ^ symbol:
  • SO - Batch Character Escaping
  • Escape Characters
  • Escape Characters, Delimiters and Quotes

  • Echoing Parentheses in Windows Batch Files

    It’s not just about parentheses, really. It’s about almost anything that a script engine will ruthlessly interpret before executing a command: variable markers (percent signs), redirection symbols, parentheses, double quotes, ampersands…

    My most common… let’s say, case, is this:

    @echo off
    
    echo Doing something (very important)...
    

    Put this in a BATch file, run it, and you’ll end up with a message saying

    ... was unexpected at this time.
    

    No, seriously? It’s great that the engine is trying to evaluate an expression within an echo, but this time I need something much more simple: round brackets embedded into a text message. Just that. Please.

    Thank heavens there is a “cure”: parentheses can be escaped with a caret character.

    @echo off
    
    echo Doing something ^(very important^)...
    

    Now we’re good:

    Doing something (very important)...
    

    Carets can be used to escape almost any special character, even a newline — to break a single command in several lines.

    source

5
  • Already tried to escape the dot, with the same result. (Pasted the line you provided, too - no change.) Jul 1, 2016 at 17:05
  • 1
    Escaping the parentheses on top of the dot did it! Thank you. Jul 1, 2016 at 17:16
  • Can you explain why this needs to be done? What is going on here? Jul 25, 2017 at 20:44
  • @OhadSchneider I just added some reference and further resources and quoted a few simple explanations from some of those sources. Please see my updated answer when you get a chance as it may further help clarify for readers moving forward with this regard. Thank you for pointing that out to me and asking the question. I noticed that the article I quoted is the one you found too so I think it does help clarify with the regard you are talking about so nice find there too btw. I noticed after I put it on my answer too ... Jul 25, 2017 at 21:18
  • @McDonald's great job! (sorry for the late response) Aug 1, 2017 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .