need to echo literal values like "%1" and ">"

  • Thread starter Thread starter TJT
  • Start date Start date
T

TJT

I am trying to have a BAT file generate another BAT file. In that target
BAT file, I would like to write values like:

CALL MyBatFile.bat %1 %2 > MyTargetOutputFile

I tried executing the following command...

echo "CALL MyBatFile.bat %1 %2 > MyTargetOutputFile" >> NewBatFile.bat

Of course the %1 and %2 are getting substituted (I just want the literal
values %1 and %2 written). Furthermore the first > tries to pipe the output
to MyTargetOutputFile (I also wanted "> MyTargetOutputFile" to be written to
the NewBatFile.bat.

Is there any way to do what I want?

Thanks,
Tom
 
TJT said:
I tried executing the following command...
echo "CALL MyBatFile.bat %1 %2 > MyTargetOutputFile" >> NewBatFile.bat
Is there any way to do what I want?

echo "CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile" >>
NewBatFile.bat

(Watch out for the potential wrapping.)

All the best, Timo
 
TJT said:
I am trying to have a BAT file generate another BAT file. In that target
BAT file, I would like to write values like:

CALL MyBatFile.bat %1 %2 > MyTargetOutputFile

I tried executing the following command...

echo "CALL MyBatFile.bat %1 %2 > MyTargetOutputFile" >> NewBatFile.bat

Of course the %1 and %2 are getting substituted (I just want the literal
values %1 and %2 written). Furthermore the first > tries to pipe the output
to MyTargetOutputFile (I also wanted "> MyTargetOutputFile" to be written to
the NewBatFile.bat.

Is there any way to do what I want?

Thanks,
Tom

In addition to using two percent signs, precede the '>' with a caret '^'.

echo/call MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile >> NewBatFile.bat
 
In addition to using two percent signs, precede the '>' with a caret '^'.
echo/call MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile >> NewBatFile.bat

Phil, with you I feel safe to comment on the respective solutions
that we have posted. I posted

echo "CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile" >>
NewBatFile.bat

Your solution is the correct one. However, this brings up an
interesting aside. Run and take a look what in produced into
NewBatFile.bat if one uses

echo "CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile" >>
NewBatFile.bat

The behavior of the caret changes! With the quotes one needs %>
instead of ^>

What happened here is that I only tested the first step, but not the
second, that is the behavior of MyBatFile.bat which breakes in my
version.

"These are tricky tasks". But I don't mind being found out when I am
wrong or careless. This time I was and readily admit it.

All the best, Timo
 
Timo said:
Phil, with you I feel safe to comment on the respective solutions
that we have posted. I posted

echo "CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile" >> NewBatFile.bat

Your solution is the correct one. However, this brings up an
interesting aside. Run and take a look what in produced into
NewBatFile.bat if one uses

echo "CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile" >> NewBatFile.bat

The behavior of the caret changes! With the quotes one needs %>
instead of ^>

What happened here is that I only tested the first step, but not the
second, that is the behavior of MyBatFile.bat which breakes in my
version.

"These are tricky tasks". But I don't mind being found out when I am
wrong or careless. This time I was and readily admit it.

All the best, Timo

Hi, Timo,

I'm not entirely sure I follow you. Here's what I get with the various
variations:

- - - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - - -
c:\cmd>echo "CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile" >>
NewBatFile.bat

c:\cmd>echo "CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile" >>
NewBatFile.bat

c:\cmd>echo CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile >>
NewBatFile.bat

c:\cmd>echo CALL MyBatFile.bat ^%1 ^%2 ^> MyTargetOutputFile >>
NewBatFile.bat

c:\cmd>type newbatfile.bat
"CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile"
"CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile"
CALL MyBatFile.bat %%1 %%2 > MyTargetOutputFile
CALL MyBatFile.bat %1 %2 > MyTargetOutputFile
- - - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - - -

So it seems to me, at least on my system, that the last variation is
what the OP would want. Or maybe I misread the OP.
 
I'm not entirely sure I follow you. Here's what I get with the
various variations:
c:\cmd>echo "CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile" >>
NewBatFile.bat :

c:\cmd>type newbatfile.bat "CALL MyBatFile.bat %%1 %%2 ^>
MyTargetOutputFile"
So it seems to me, at least on my system, that the last variation
is what the OP would want. Or maybe I misread the OP.

Phil, you solved the OP's problem correctly. I made a mistake in my
solution. Let's look it from another angle, beyond.

You have the test in the above on the command line. For demonstration
let's put put it into yet another script:

@echo off
echo "CALL MyBatFile.bat %%1 %%2 ^> MyTargetOutputFile" >>
NewBatFile.bat
type NewBatFile.bat

The output:
C:\_M>test
"CALL MyBatFile.bat %1 %2 ^> MyTargetOutputFile"

Note the caret remaining! Now next

@echo off
echo "CALL MyBatFile.bat %%1 %%2 %> MyTargetOutputFile" >>
NewBatFile.bat
type NewBatFile.bat

The output
C:\_M>test
"CALL MyBatFile.bat %1 %2 > MyTargetOutputFile"

The caret disappears. BUT one can't run the result because it looks
for a script within the entire quotes. That was my mistake. On the
other hand it shows the difference between the escape methods under
different circumstances.

What I had later in my testing in MyBatFile.bat was
@echo off
echo Hello World
It completes testing for the original problem. It only works without
the quotes I used!

All the best, Timo
 
Back
Top