How to put %random% to a new batch file with a bat

2020-02-02 03:25发布

I want to but this in to an other .bat file

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

So I have tried with this:

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

But the %% things don't work (it will put a random nummer but I want to have %random% in the new .bat file

1条回答
地球回转人心会变
2楼-- · 2020-02-02 04:11

you have to escape some chars if you want to write them literally. Most of them are escaped with a caret ^ (&,<,>,|). The percent sign is an exception: it is escaped with another percent-sign:`

echo %%random%% ^> test.bat

But may I suggest another method to write your second bat file (without caring for characters to escape):

@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

The for is used to determine the start line of the data to be written, more +n writes the file ("~dpnx0" is the currently running batchfile) by skipping the first n lines.

Pro: no need to escape anything - just write the data as it should be outputted.

Contra: only one "second file" possible; only static text possible.

查看更多
登录 后发表回答