Echoing Special Characters properly

A

Albert Fuchigami

Hi There,

I posed in a different thread about how to get special characters
included as input to a batch file. Thanks to the responses from the
group, I've got a script that lets me include the 'pipe' symbol (|) as
part of the input, and I've assigned it to an environment variable.

Now I'm finding that under some circumstances, the variable doesn't
get displayed properly by the echo statement. For example, if you
have double-quotes in the echo statement, the pipe character is
printed as ^| instead of |.

Here's a small test file to illustrate the problem:
@echo off
setlocal
set myparm=%~1
echo myparam is %myparm%
echo myparm1 is '%myparm%'
echo myparam2 is "This is '%myparm%'"

When I run the batch file, here are the results:
C:\>SpecialChars.cmd "A^^^|B"
myparam is A|B
myparm1 is 'A|B'
myparam2 is "This is 'A^|B' value"

Does anyone know how I can include double-quotes in the echo statement
and get the proper output? (i.e. myparam2 is "This is 'A|B' value")
String substitution doesn't seem to have any effect, and I'm not even
sure how I would use it to avoid the problem.

Thanks.

Albert.
 
J

Jerold Schulman

For your example:

@echo off
setlocal
set myparm=%~1
set myparm=%myparm:^^=%
echo myparam is %myparm%
echo myparm1 is '%myparm%'
echo myparam2 is "This is '%myparm%'"
endlocal

C:\>test1 "A^^^B"
myparam is AB
myparm1 is 'AB'
myparam2 is "This is 'AB'"




Hi There,

I posed in a different thread about how to get special characters
included as input to a batch file. Thanks to the responses from the
group, I've got a script that lets me include the 'pipe' symbol (|) as
part of the input, and I've assigned it to an environment variable.

Now I'm finding that under some circumstances, the variable doesn't
get displayed properly by the echo statement. For example, if you
have double-quotes in the echo statement, the pipe character is
printed as ^| instead of |.

Here's a small test file to illustrate the problem:
@echo off
setlocal
set myparm=%~1
echo myparam is %myparm%
echo myparm1 is '%myparm%'
echo myparam2 is "This is '%myparm%'"

When I run the batch file, here are the results:
C:\>SpecialChars.cmd "A^^^|B"
myparam is A|B
myparm1 is 'A|B'
myparam2 is "This is 'A^|B' value"

Does anyone know how I can include double-quotes in the echo statement
and get the proper output? (i.e. myparam2 is "This is 'A|B' value")
String substitution doesn't seem to have any effect, and I'm not even
sure how I would use it to avoid the problem.

Thanks.

Albert.


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top