批处理文件创建另一个批处理文件,如何写行时忽略的命令? [英] Batch file creating another batch file, how to ignore commands when writing lines?
问题描述
有一点麻烦与Windows批处理文件,我写。
having a bit of trouble with a windows batch file I'm writing.
我需要批处理文件写一些特殊的行另一个批处理文件,我一直使用的方法是:
I need the batch file to write some particular lines to another batch file, the method I have been using is:
type NUL > batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
ECHO texttobewrittentofile >> batchfile.bat
...
etc
大部分的线条细写,还有我和我的批处理文件写入到另一个批处理文件,虽然有几个不同的问题。
Most of the lines write fine, there are a few different problems I'm having with my batch file writing to another batch file though.
在code为我的批处理文件写入到另一个批处理文件是:
The code for my batch file to write to another batch file is:
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'm not getting the desired output that I would like, I'm having issues with:
ECHO Y | XCOPY的O:\\日志\\%CLIENTNAME%\\%最新%。\\>> GenerateEmail.bat
现身为
0文件(S)
和无处不在,我需要%%它出来为%,以及该行开始blat.exe完全不除其他问题编写的。有没有什么办法让一个批处理文件中写入文本行而不承认包含在这些线路的任何命令或符号?
and everywhere that I need %% it comes out as %, as well as the line starting with "blat.exe" isn't written at all amongst other problems. Is there any way to get a batch file to write lines of text without acknowledging any commands or symbols contained in those lines?
凯恩。
推荐答案
所有的特殊字符,如 ^
&安培;
|
<
>
必须转义为 ^ ^
^&安培;
^ |
^<
^>
或者用引号括起来。
All special characters like ^
&
|
<
>
must be escaped as ^^
^&
^|
^<
^>
or else enclosed in quotes.
每一个百分比必须始终一倍。例如:
Every percent must be doubled always. For example:
-
%1
变成%% 1
-
最新%%
变成%%最新%%
-
%% A
变成%%%%一个
"%1"
becomes"%%1"
%newest%
becomes%%newest%%
%%a
becomes%%%%a
您不需要清除输出文件和重定向的每一行,如果你把周围所有的echo语句括号。右括号也必须这样做,如果这个逃脱了。
You don't need to clear the output file and redirect each line if you put parentheses around all the ECHO statements. Right parentheses must also be escaped if doing this.
>GenerateEmail.bat (
echo Line 1
echo Line 2
echo these must be escaped ^^ ^& ^| ^> ^< ^), these not "^ & | > < )"
)
如果您已经推迟的规则变得更加复杂启用扩展,并且拥有包括输出!
或!
与 ^
。
The rules become more complicated if you have delayed expansion enabled and you have output that includes !
, or !
with ^
.
有这避免弄清楚如何逃生特殊字符和加倍百分比等方法。
There are other methods that avoid having to figure out how to escape special characters and double up percents.
您可以preFIX与每行:::
和FOR / F使用带有FINDSTR以获得所需的输出。确保推迟扩张已关闭,如果你的输出包含!
。与下面的code的唯一限制就是你的输出线不能与开始:
(它可以用一个空格,后面开始:
)。
You can prefix each line with :::
and use FOR /F with FINDSTR to get the desired output. Make sure delayed expansion is off if your output contains !
. The only restriction with the code below is your output line cannot begin with :
(it can begin with a space, followed by :
).
(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 techniques found at:
- Echo the contents of a paragraph into a file without any modifications to the paragraph?
- printing a paragraph in windows batch
这篇关于批处理文件创建另一个批处理文件,如何写行时忽略的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!