Escaping parentheses within parentheses for batch file

37,590

Solution 1

It's obvious that you need three carets there :-)

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something (like this^^^)
)|clip

Why? That is a bit tricky...

First the parser parses the block and escapes the part this^^^) to this^), the parenthesis was escaped here the first time.

But as you used a pipe, the complete block will be translated and transfered to a new cmd.exe instance.
C:\Windows\system32\cmd.exe /S /D /c" ( @ echo This is some code is ... & @ echo for something (like this^) )"

And in the new instance it is necessary again to escape the closing parenthesis.
That's all!

And for more information you could read a similar question
SO: why does delayed expansion fail when inside a piped block of code

Solution 2

If you get confused by the caret count, remember that it is always a power of 2 minus 1. Alternatively, remember that the first time you add 2, the second time 4, the third time 8, ...

For example, (tested on XP)

(
for /f "tokens=* delims=" %%S in (
'echo X^^^^^^^^^^^^^^^& ^^^^^^^^^^^^^^^| ^^^| find " "'
) do @echo %%S
) | find "X"

The echo statement gets read first normally when DOS parses the batch file (1), second because it is a piped internal command (DOS seems to want programs in pipes, and internal commands are not programs) (3), third because it is in a 'for /f' command-driven word list (see for /?) (7), and a fourth time for reasons already given in this thread (15).

I may also note that personally, I consider it sloppy coding to leave proper escaping of hot characters away just because DOS happens to ignore the missing escaping in that particular case. So if I wanted to echo a literal pair of parentheses in the above example, I would give each 15 carets, even if the opening parenthesis does not need it. Then anyone reading my code does not have to wonder why one parenthesis is quoted and the other is not. And how to modify the quoting under code modification.

Solution 3

My solution when I need to use a closing parenthesis in my encapsulated echos is to create a variable named %endPar% at the beginning of my script, something like this:

@set "endPar=)"
@echo This is some code that is>tmp
@echo Important to echo exactly as-is>>tmp
@echo Even if I use parenthesis>>tmp
@echo for something (like this%endPar%>>tmp
@type tmp|clip
@del tmp

I changed it a bit, and tested it twice.

The code simply outputs the echos to a file called tmp, you then pipe the contents of the file to clip, and it's ready for you to paste wherever you want.

Solution 4

The triple-caret does not work in all situations. take the following:

IF EXIST file.txt (
ECHO ...OK
) ELSE (
ECHO TEST THIS: (SOME WORDING! ^^^)   > file.txt
)

the contents of file.txt are:

TEST THIS: (SOME WORDING! ^)
Share:
37,590

Related videos on Youtube

user1759622
Author by

user1759622

Updated on September 12, 2020

Comments

  • user1759622
    user1759622 over 3 years

    This is what I am trying to do:

    (
    @echo This is some code that is
    @echo Important to echo exactly as-is
    @echo Even if I use parenthesis
    @echo for something (like this)
    )|clip
    

    So having that copied into clipboard. I have tried the following:

    (
    @echo This is some code that is
    @echo Important to echo exactly as-is
    @echo Even if I use parenthesis
    @echo for something ^(like this^)
    )|clip
    

    But this seems to affect my WANTED parentheses as well because I receive an error. To test this, I simply done:

    (
    @echo This is some code that is
    @echo Important to echo exactly as-is
    @echo Even if I use parenthesis
    @echo for something ^(like this^)
    FFF
    )|clip
    

    Hey! This almost works, the text is copied all the way to the "s" in "this". It does not copy the last parentheses, or obviously the F's. Any ideas?

    • LittleBobbyTables - Au Revoir
      LittleBobbyTables - Au Revoir over 11 years
      I don't understand the problem. What error are you receiving? If I use @echo for something ^(like this^), it echos out for something (like this).
    • user1759622
      user1759622 over 11 years
      That comment by itself is fine. The escape character seems to also affect the second parentheses, the one for ")|clip". If you try using that entire code you'll see what I mean. I receive ) was unexpected at this time.
  • LittleBobbyTables - Au Revoir
    LittleBobbyTables - Au Revoir over 11 years
    Hah, I tried with two carats and it didn't work either, and I didn't get around to trying three carats. Also, I think you want @echo for something ^(like this^^^).
  • jeb
    jeb over 11 years
    @LittleBobbyTables - No, you only need carets for the closing parenthesis
  • user1759622
    user1759622 over 11 years
    Thanks man, you're the best. I wish I understood how that works, but anyway, thanks for the answer!
  • jeb
    jeb over 10 years
    Yes, but I described why the solution needs three carets, as the block is piped and therefore a single caret isn't enough
  • DaveRandom
    DaveRandom over 9 years
    Srsly, Microsoft. What the actual F.
  • jeb
    jeb over 8 years
    But it doesn't work. The closing parenthesis is missing
  • Sirach Matthews
    Sirach Matthews over 8 years
    @jeb - you're absolutely correct, it didn't work. Silly me. I changed it up a bit, and tested it a few times. Try it now.
  • jeb
    jeb over 8 years
    Now it works, but it doesn't answer the question anymore. How to escape parenthesis inside of parenthesis
  • Sirach Matthews
    Sirach Matthews over 8 years
    @jeb Too true. My original answer works fine in most cases without the pipe, but once piping is involved, triple-carrots all the way. Were my reputation high enough, I'd up-vote your answer in a heart-beat.