2016-04-17 157 views
0

我想,但这在一个其他.bat文件如何将%random%添加到批处理文件的新批处理文件中?

echo %random% > test.bat 
set /a num= +1 
:repeat1 
set /a num= +1 
echo %random% >> test.bat 
if ==100 goto end 
goto repeat1 

所以我这个尝试:

echo echo %every%%random%%random%%random%%random% > "output.txt" > test.bat 
echo set /a num=%num% +1 >> test.bat 
echo :repeat1 >> test.bat 
echo set /a num=%num% +1 >> test.bat 
echo echo %every%%random%%random%%random%%random% >> "output.txt" >> test.bat 
echo if %num%==%amount% goto end >> test.bat 
echo goto repeat1 >> test.bat 
:end >> test.bat 

但%%事情不工作(它将把一个随机nummer但我想有随机%% .bat文件

+0

所以'>“output.txt”'应该在新的批处理文件中直接?你需要用'>>'来转义''''''然后写''> output.txt“>”test.bat“'...'>>”output.txt“'也是如此,其中'>>'变成'^> ^>'... – aschipfl

+0

“但%%的东西不起作用” - 你没有任何'%%'的东西。你有'%every%'(我们可以看到它是空的),然后是'%random%',另一个'%random%'等等。你应该有'%% random %%%% random %%%% random %%%% random %%' – Stephan

回答

1

你要逃避一些字符,如果你想给他们字面上写。他们大多逃脱一个脱字号^&,<,>,|)。百分号是一个例外:它逃脱了另一个百分号:`

echo %%random%% ^> test.bat 

但我可以建议其他方法来写你的第二个批处理文件(没有照顾字符逃跑):

@echo off 
for /f "delims=:" %%i in (' findstr /n /c:":: Start of Second ::" "%~dpnx0"') do set start=%%i 
more +%start% "%~dpnx0" >test.bat 
call test.bat 
echo ------- 
type test.txt 
echo ------- 

goto :eof 
:: Start of Second :: 
@echo off 
echo %random% >test.txt 
set /a num=0 
:repeat1 
    set /a num+=1 
    echo %random% >>test.txt 
    if %num%==10 goto :eof 
goto .repeat1 

for用于确定要写入的数据的起始行, more +n通过跳过第一行n行来写入文件("~dpnx0"是当前正在运行的批处理文件)。

亲:无需转义任何东西 - 只需写入数据,因为它应该输出。

Contra:只有一个“第二个文件”可能;只有静态文本可能。

相关问题