Brans Ds
Brans Ds

Reputation: 4117

Batch character escape on ^| don't work

^| Should be escape for the | character.

set good=Debug^|Win32

echo Incorrect paramets %good%

pause > nul

Why this give me error?

Upvotes: 1

Views: 186

Answers (7)

Endoro
Endoro

Reputation: 37569

use set/p for output:

@echo off&setlocal
set "good=Debug|Win32"
<nul set/p"=Incorrect parameters %good%"
echo(

output is:

Incorrect parameters Debug|Win32

Upvotes: 1

jeb
jeb

Reputation: 82297

I try to add some explanations to the main problem.

As James L. noted, you have the expected content in your variable, but it's problematic to echo it.

echo Incorrect parameters %good:|=^|%

It's a problem of the parser.
After a percent expansion special characters will be parsed, if they aren't escaped by a caret or quote.

The way of James L. only works if there are only pipes, but no other special characters in the variable, as you can't replace two different characters this way.

But you could escape all special characters twice, like:

set good=Debug^^^|Win32^^^&Ampersand
echo Incorrect parameters %good%

or the same

set "good=Debug^|Win32^&Ampersand"
echo Incorrect parameters %good%

This works, but the content of good is Debug^|Win32^&Ampersand which is in the most cases not wanted.

The way of Aacini, to use delayed expansion works always with any content, as after the delayed expansion the content will not further be parsed, so the special characters are harmless.
This is one of the major advantages of the delayed expansion about percent expansion.

There is another way to use quotes like the solution of Axel Kemper.

echo "Incorrect parameters %good%"

But in the most cases the quotes shouldn't be printed.

Upvotes: 3

Endoro
Endoro

Reputation: 37569

I would recommend:

echo Incorrect paramets Debug^|Win32
pause > nul

Upvotes: 0

Aacini
Aacini

Reputation: 67216

You have two ways to insert special characters in a variable. Escaping them:

set good=Debug^|Win32

or enclosing them between quotes:

set "good=Debug|Win32"

However, the only way to correctly show a variable value that contain special characters is using delayed expansion:

setlocal EnableDelayedExpansion
set good=Debug^|Win32
echo Incorrect paramets !good!
pause > nul

Upvotes: 4

James L.
James L.

Reputation: 9451

You properly escaped the | character by set good=Debut^|Win32. When you echo it, you have to replace the | character with the escape sequence, like this:

@echo off
set good=Debug^|Win32

echo Incorrect paramets %good:|=^|%

pause > nul

This makes it echo the escaped pipe (^\) instead of piping it to Win32.

Upvotes: 1

Axel Kemper
Axel Kemper

Reputation: 11322

The following works as intended:

set good=Debug^|Win32

echo "Incorrect parameters %good%"

pause > nul

If you omit the quotes in the echo statement, Windows tries to execute "Win32" which is impossible.

Upvotes: 0

Magoo
Magoo

Reputation: 80023

Try using a label.

In batch, labels are preceded by the colon.

:ok

is a label.

It is not necessary to usu the colon in a goto

goto ok
goto :ok

are equivalent.

Upvotes: 1

Related Questions