Specifying special characters as input to a batch file

A

Albert Fuchigami

Hi There,

I need to pass in some strings that contains special characters to a
batch file. (The strings are then being passed on to another
application). Is there any way of telling the command processor to
just treat them literally?

For example, if I run the following
Prompt>Test.bat "A|B"

I want a local environment variable inside the batch file to get the
value A|B.

I've tried "A|B", "A^|B", "A||B" (all with and without the
double-quotes) without any success. The batch file either errors out,
or doesn't get the value I'm looking for.

Does anyone have any ideas?

Thanks.

Albert.
 
P

Phil Robyn

Albert said:
Hi There,

I need to pass in some strings that contains special characters to a
batch file. (The strings are then being passed on to another
application). Is there any way of telling the command processor to
just treat them literally?

For example, if I run the following
Prompt>Test.bat "A|B"

I want a local environment variable inside the batch file to get the
value A|B.

I've tried "A|B", "A^|B", "A||B" (all with and without the
double-quotes) without any success. The batch file either errors out,
or doesn't get the value I'm looking for.

Does anyone have any ideas?

Thanks.

Albert.

C:\cmd>demo\SpecialChars "A^^^|B"
myparm is [A|B]

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. setlocal
3. set myparm=%~1
4. echo myparm is [%myparm%]
=====end C:\cmd\demo\SpecialChars.cmd ====================
 
M

Matthias Tacke

Phil said:
Albert said:
Hi There,

I need to pass in some strings that contains special characters to a
batch file. (The strings are then being passed on to another
application). Is there any way of telling the command processor to
just treat them literally?

For example, if I run the following
Prompt>Test.bat "A|B"

I want a local environment variable inside the batch file to get the
value A|B.

I've tried "A|B", "A^|B", "A||B" (all with and without the
double-quotes) without any success. The batch file either errors out,
or doesn't get the value I'm looking for.

Does anyone have any ideas?

Thanks.

Albert.

C:\cmd>demo\SpecialChars "A^^^|B"
myparm is [A|B]

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. setlocal
3. set myparm=%~1
4. echo myparm is [%myparm%]
=====end C:\cmd\demo\SpecialChars.cmd ====================

Hello Phil,
I thought this was your idea:

C:\cmd>demo\SpecialChars "A|B"
myparm is [A|B]
C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. set /P ="myparm is [%~1]"<NUL
=====end C:\cmd\demo\SpecialChars.cmd ====================
 
M

Matthias Tacke

Matthias Tacke said:
Phil Robyn wrote:
Hello Phil,
I thought this was your idea:

C:\cmd>demo\SpecialChars "A|B"
myparm is [A|B]
C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. set /P ="myparm is [%~1]"<NUL
=====end C:\cmd\demo\SpecialChars.cmd ====================

I missed the point with the var,
my suggestion is only for output without escaping.
 
P

Phil Robyn

Matthias said:
Phil Robyn wrote:

Albert Fuchigami wrote:

Hi There,

I need to pass in some strings that contains special characters to a
batch file. (The strings are then being passed on to another
application). Is there any way of telling the command processor to
just treat them literally?

For example, if I run the following
Prompt>Test.bat "A|B"

I want a local environment variable inside the batch file to get the
value A|B.

I've tried "A|B", "A^|B", "A||B" (all with and without the
double-quotes) without any success. The batch file either errors out,
or doesn't get the value I'm looking for.

Does anyone have any ideas?

Thanks.

Albert.

C:\cmd>demo\SpecialChars "A^^^|B"
myparm is [A|B]

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. setlocal
3. set myparm=%~1
4. echo myparm is [%myparm%]
=====end C:\cmd\demo\SpecialChars.cmd ====================


Hello Phil,
I thought this was your idea:

C:\cmd>demo\SpecialChars "A|B"
myparm is [A|B]
C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. set /P ="myparm is [%~1]"<NUL
=====end C:\cmd\demo\SpecialChars.cmd ====================

Yeah, I forgot about that one! Thanks for reminding me! :)
 
P

Phil Robyn

Matthias said:
Phil Robyn wrote:

Albert Fuchigami wrote:

Hi There,

I need to pass in some strings that contains special characters to a
batch file. (The strings are then being passed on to another
application). Is there any way of telling the command processor to
just treat them literally?

For example, if I run the following
Prompt>Test.bat "A|B"

I want a local environment variable inside the batch file to get the
value A|B.

I've tried "A|B", "A^|B", "A||B" (all with and without the
double-quotes) without any success. The batch file either errors out,
or doesn't get the value I'm looking for.

Does anyone have any ideas?

Thanks.

Albert.

C:\cmd>demo\SpecialChars "A^^^|B"
myparm is [A|B]

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. setlocal
3. set myparm=%~1
4. echo myparm is [%myparm%]
=====end C:\cmd\demo\SpecialChars.cmd ====================


Hello Phil,
I thought this was your idea:

C:\cmd>demo\SpecialChars "A|B"
myparm is [A|B]
C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. set /P ="myparm is [%~1]"<NUL
=====end C:\cmd\demo\SpecialChars.cmd ====================

Actually, to assign "A|B" to variable "myparm", I guess it would have to be

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. set /P ="%~1"<NUL>myparm.
3. set /P myparm=<myparm.&del myparm.
=====end C:\cmd\demo\SpecialChars.cmd ====================
 
A

Albert Fuchigami

On Tue, 20 Jan 2004 15:38:54 -0800, Phil Robyn

C:\cmd>demo\SpecialChars "A^^^|B"
myparm is [A|B]

C:\cmd>rlist demo\SpecialChars.cmd
=====begin C:\cmd\demo\SpecialChars.cmd ====================
1. @echo off
2. setlocal
3. set myparm=%~1
4. echo myparm is [%myparm%]
=====end C:\cmd\demo\SpecialChars.cmd ====================

That did the trick. Interestingly, the batch file has to have a .CMD
extension to work properly. If it has a .BAT extensions, it won't
work.

Also, do you know if you can do character substitution on the
variable?

I put the following line in the code:
set myparam=%myparam:A:AA%
and the input is the same as the output.

Thanks.

Albert.
 
M

Matthias Tacke

Albert Fuchigami wrote:

Also, do you know if you can do character substitution on the
variable?

I put the following line in the code:
set myparam=%myparam:A:AA%
and the input is the same as the output.
The proper syntax is : %PATH:str1=str2%

The equal sign has to be placed between the search and the replace
string.

HTH
 
A

Albert Fuchigami

On Wed, 21 Jan 2004 16:36:19 +0100, "Matthias Tacke"

The proper syntax is : %PATH:str1=str2%

The equal sign has to be placed between the search and the replace
string.

Oops. Thanks.

I tried it out and it looks like the substitution works, but then you
don't seem to be able to access the variable anymore after that with
the Echo statement. Here's a modified batch file to illustrate:

0. @echo off
1. setlocal
2. set myparm=%~1
3. echo myparam is %myparm%
4. set myparm=%myparm:A=AA%
5. echo myparamA is %myparm%
6. echo myparam1 is '%myparm%'
7. echo myparam2 is "This is '%myparm%' value"

When I run it, I find the batch file errors out on line 5 with 'B' is
not recognized as an internal or external command, operable program or
batch file.

Any ideas?

Thanks.

Albert.
 
M

Matthias Tacke

Albert said:
On Wed, 21 Jan 2004 16:36:19 +0100, "Matthias Tacke"



Oops. Thanks.

I tried it out and it looks like the substitution works, but then you
don't seem to be able to access the variable anymore after that with
the Echo statement. Here's a modified batch file to illustrate:

0. @echo off
1. setlocal
2. set myparm=%~1
3. echo myparam is %myparm%
4. set myparm=%myparm:A=AA%
5. echo myparamA is %myparm%
6. echo myparam1 is '%myparm%'
7. echo myparam2 is "This is '%myparm%' value"

When I run it, I find the batch file errors out on line 5 with 'B' is
not recognized as an internal or external command, operable program or
batch file.

Every change of a var removes escape chars so you can't see this as a
static workaround. Depending on the number of steps you have to take
care the escaping is set proper according to this.

The way with the set /p can deal without any escape chars just with
quoting. See Phil's example with redirection to a file.

You can see what happens by inserting echo on before the second set.

To learn the basics, you may follow the hint in my signature.
 

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