Batch file creating another batch file, how to ignore commands when writing lines?

with a few problems with the window file I am writing.

I need a batch file to write some lines to another batch file, the method I used:

type NUL > batchfile.bat ECHO texttobewrittentofile >> batchfile.bat ECHO texttobewrittentofile >> batchfile.bat ECHO texttobewrittentofile >> batchfile.bat ... etc 

Most lines are written well, there are several different problems that I have with writing a batch file to another batch file.

Code for my batch file to write to another batch file:

 ECHO @echo off >> GenerateEmail.bat ECHO ECHO Opening Stunnel >> GenerateEmail.bat ECHO pushd .\stunnel\ >> GenerateEmail.bat ECHO start "" stunnel.exe stunnel.conf >> GenerateEmail.bat ECHO popd >> GenerateEmail.bat ECHO ECHO Determining latest log for use with blat >> GenerateEmail.bat ECHO pushd O:\Logs\%clientname%\ >> GenerateEmail.bat ECHO for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a >> GenerateEmail.bat ECHO popd >> GenerateEmail.bat ECHO ECHO Generating email containing contents of latest log >> GenerateEmail.bat ECHO pushd .\blat307\full\ >> GenerateEmail.bat ECHO ECHO Y | xcopy "O:\Logs\%clientname%\%newest%" ".\" >> GenerateEmail.bat ECHO blat.exe "%newest%" -to %clientemail% -cc %gmailemail% -server 127.0.0.1:1099 -subject "Offsite Backup for "%1" "%2" - %clientname%" -sig sig.txt >> GenerateEmail.bat ECHO DEL .\*.log >> GenerateEmail.bat ECHO popd >> GenerateEmail.bat ECHO ECHO Closing Stunnel >> GenerateEmail.bat ECHO pushd .\stunnel\ >> GenerateEmail.bat ECHO stunnel.exe -exit >> GenerateEmail.bat ECHO popd >> GenerateEmail.bat 

I do not get the desired result that I would like, I have problems with:

ECHO Y | xcopy "O:\Logs\%clientname%\%newest%" ".\" >> GenerateEmail.bat

comes out like

0 File(s) copied

and wherever I need %%, it appears as%, and the line starting with "blat.exe" is not written at all among other problems. Is there a way to get a batch file to write lines of text without confirming any commands or characters contained in these lines?

Kane.

+4
source share
1 answer

All special characters such as ^ & | < > must be escaped as ^^ ^& ^| ^< ^> or are enclosed in quotation marks.

Each percentage should always be doubled. For instance:

  • "%1" becomes "%%1"
  • %newest% becomes %%newest%%
  • %%a becomes %%%%a

You do not need to clear the output file and redirect each line if you put parentheses in all ECHO statements. In addition, the right parentheses must be escaped.

 >GenerateEmail.bat ( echo Line 1 echo Line 2 echo these must be escaped ^^ ^& ^| ^> ^< ^), these not "^ & | > < )" ) 

Rules become more complex if you enable extension delay, and you have a conclusion that includes ! or ! s ^ .

There are other methods that do not allow you to figure out how to avoid special characters and double percent.

You can prefix each line ::: and use FOR / F with FINDSTR to get the desired result. Make sure slow-motion expansion is disabled if your output contains ! . The only limitation with the code below is that your output line cannot begin with : (it can start with a space, and then : .

 (for /f "tokens=* delims=:" %%L in ('findstr /b ::: "%~f0"') do echo %%L) >GenerateEmail.bat :::echo special chars & | < > don't need to be escaped :::echo % does not need to be doubled ::: :Label ::: :: Comment and label must be indented with at least one space :::etc 

Another option is to use one of the methods found at:

+8
source

All Articles