Question

I am trying to have a batch file create another batch file that can be called by the first batch file.

I need to have it ouput a for loop using tokens. For variables I am able to add an extra % on both ends for it to output %variable% to the file. However %%i comes out as %i (only one % sign). Ive tried the carat, parenthesis, moving the riderects, quotes, extra % signs etc... for the life of me can not figure out how to make it just print %%i to file.

Sample code:

echo FOR /f "delims=" %%i IN ('commands') DO CALL :process %%i > temp.bat

But I end up getting the following no matter what I try:

echo FOR /f "delims=" %i IN ('commands') DO CALL :process %i > temp.bat

How do I get around this issue? is there a better way to redirect text than echo > file ?

Thanks!

Était-ce utile?

La solution

% escapes %. Double-up each and every % that you want to be reproduced literally.

@ECHO OFF
SETLOCAL
echo FOR /f "delims=" %%i IN ('commands') DO CALL :process %%i > temp.bat
echo FOR /f "delims=" %%%%i IN ('commands') DO CALL :process %%%%i >> temp.bat
echo FOR /f "delims=" %%%%i IN ('commands') DO CALL :process %%%%i ^>^> temp.bat >> temp.bat
GOTO :EOF

result

FOR /f "delims=" %i IN ('commands') DO CALL :process %i 
FOR /f "delims=" %%i IN ('commands') DO CALL :process %%i 
FOR /f "delims=" %%i IN ('commands') DO CALL :process %%i >> temp.bat 

salutary lesson in line-terminal spaces; Each line produced in this manner will contain a terminal space because of the space between the required text and the filename-redirector. Moving the redirector and filename to the beginning of the line will reproduce only REAL terminal-spaces

>> temp.bat echo FOR /f "delims=" %%%%i IN ('commands') DO CALL :process %%%%i
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top