2016-04-17 154 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:只有一個「第二個文件」可能;只有靜態文本可能。

相關問題