Redirection

G

Guest

I'm trying to echo a for loop to a .bat file and am having trouble getting
the percent signs to go. For example...

echo for /f %%A in ('command') do set var=%%A >test.bat

Would make test.bat look like...

for /f %A in ('command') do set var=%A

It takes off one of the percent signs. I've tried using ^ multiple different
ways but have not had any luck. Does anyone know how I would be able to echo
the for loop properly?

Thanks,

Mr. Novice.
 
D

Dean Wells [MVP]

What OS and shell are you using? Here's my result using CMD.EXE on
Windows XP SP2 -

C:\>echo for /f %%A in ('command') do set var=%%A >test.bat

C:\>type test.bat
for /f %%A in ('command') do set var=%%A

.... are you issuing the command within another shell script?
 
A

Al Dunbar [MS-MVP]

Dean Wells said:
What OS and shell are you using? Here's my result using CMD.EXE on
Windows XP SP2 -

C:\>echo for /f %%A in ('command') do set var=%%A >test.bat

C:\>type test.bat
for /f %%A in ('command') do set var=%%A

... are you issuing the command within another shell script?

Apparently. I get the same result running the command from within a script.
This works, though:

echo for /f %%%%A in ('command') do set var=%%%%A >test.txt

Kind of like how you double the percent signs when using call to cause a
command to be pre-processed more than once.


/Al
--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Mr. Novice said:
I'm trying to echo a for loop to a .bat file and am having trouble
getting the percent signs to go. For example...

echo for /f %%A in ('command') do set var=%%A >test.bat

Would make test.bat look like...

for /f %A in ('command') do set var=%A

It takes off one of the percent signs. I've tried using ^ multiple
different ways but have not had any luck. Does anyone know how I
would be able to echo the for loop properly?

Thanks,

Mr. Novice.
 
B

billious

Dean Wells said:
Mr. Novice said:
I'm trying to echo a for loop to a .bat file and am having trouble
getting the percent signs to go. For example...

echo for /f %%A in ('command') do set var=%%A >test.bat

Would make test.bat look like...

for /f %A in ('command') do set var=%A

It takes off one of the percent signs. I've tried using ^ multiple
different ways but have not had any luck. Does anyone know how I
would be able to echo the for loop properly?

Thanks,

Mr. Novice.

What OS and shell are you using? Here's my result using CMD.EXE on
Windows XP SP2 -

C:\>echo for /f %%A in ('command') do set var=%%A >test.bat

C:\>type test.bat
for /f %%A in ('command') do set var=%%A

... are you issuing the command within another shell script?

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Hmm...novice indeed! Still, we all have to start somewhere...

It's not obvious what you want to do at first glance - which makes your
question harder to respond to. A novice might gain some tips from
http://support.microsoft.com/default.aspx?scid=kb;en-us;555372&sd=rss&spid=1773

What you appear to want within a batch file is to generate a batch file
containing the line

for /f %%A in ('command') do set var=%%A

which would be a sensible* command for a batch file to execute.

Dean's advice is perfectly correct BUT only works directly at the
command-prompt.

To generate that line WITHIN a batch file, you need to double each "%" thus:

echo for /f %%%%A in ('command') do set var=%%%%A >test.bat

which will generate the (presumably) required line.

(*)Two little potential problems with this:
1) because you use ">test.bat", this line will be written to 'test.bat',
overwriting the current contents (if any.) To APPEND the line to the batch,
use ">>" in place of ">"
2) the command
for /f %%A in ('command') do set var=%%A

will set 'var' to the LAST line of (whatever output "command" generates.)
This may or may not be what you have in mind.

One last tip - alt.msdos.batch.nt is a long-established batch group
supporting NT/2K/XP, and alt.msdos.batch even longer-established and aimed
at DOS/9x.

HTH

....Bill
 
G

Guest

Thanks to all three of you for your help. Putting four percent signs works
great! I'll try to be more specific next time and also check the Google
newsgroup you mentioned more. I've been there before, but have gotten such
great help from the people in this newsgroup. Dean and Al have both helped me
several times in the past (though they probably don't remember me=) ).

Thanks again guys!!

Chad


billious said:
Dean Wells said:
Mr. Novice said:
I'm trying to echo a for loop to a .bat file and am having trouble
getting the percent signs to go. For example...

echo for /f %%A in ('command') do set var=%%A >test.bat

Would make test.bat look like...

for /f %A in ('command') do set var=%A

It takes off one of the percent signs. I've tried using ^ multiple
different ways but have not had any luck. Does anyone know how I
would be able to echo the for loop properly?

Thanks,

Mr. Novice.

What OS and shell are you using? Here's my result using CMD.EXE on
Windows XP SP2 -

C:\>echo for /f %%A in ('command') do set var=%%A >test.bat

C:\>type test.bat
for /f %%A in ('command') do set var=%%A

... are you issuing the command within another shell script?

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Hmm...novice indeed! Still, we all have to start somewhere...

It's not obvious what you want to do at first glance - which makes your
question harder to respond to. A novice might gain some tips from
http://support.microsoft.com/default.aspx?scid=kb;en-us;555372&sd=rss&spid=1773

What you appear to want within a batch file is to generate a batch file
containing the line

for /f %%A in ('command') do set var=%%A

which would be a sensible* command for a batch file to execute.

Dean's advice is perfectly correct BUT only works directly at the
command-prompt.

To generate that line WITHIN a batch file, you need to double each "%" thus:

echo for /f %%%%A in ('command') do set var=%%%%A >test.bat

which will generate the (presumably) required line.

(*)Two little potential problems with this:
1) because you use ">test.bat", this line will be written to 'test.bat',
overwriting the current contents (if any.) To APPEND the line to the batch,
use ">>" in place of ">"
2) the command
for /f %%A in ('command') do set var=%%A

will set 'var' to the LAST line of (whatever output "command" generates.)
This may or may not be what you have in mind.

One last tip - alt.msdos.batch.nt is a long-established batch group
supporting NT/2K/XP, and alt.msdos.batch even longer-established and aimed
at DOS/9x.

HTH

....Bill
 

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