批处理文件使另一个批处理文件生成另一个文件

时间:2014-04-24 19:52:59

标签: windows batch-file cmd

我想编写一个批处理文件,该文件生成另一个创建文件的批处理文件。

我写了类似的东西。

echo:IF EXIST %%ALLUSERSPROFILE%%\Pulpit (SET AutoStart="%%ALLUSERSPROFILE%%\Menu Start\Programy\Autostart\go2.bat" )> %Esc_LinkTarget%\GO.BAT
echo:IF EXIST %%ALLUSERSPROFILE%%\Desktop  (SET AutoStart="%%ALLUSERSPROFILE%%\Microsoft\Windows\Start Menu\Programs\Startup\go2.bat" ) >> %Esc_LinkTarget%\GO.BAT
echo:ECHO:REM DEKLARACJA LITERY MAPOWANEGO DYSKU ^> %%AutoStart%% >> %Esc_LinkTarget%\GO.BAT
echo:ECHO:SET MapLetter=T: ^>^> %%AutoStart%% >> %Esc_LinkTarget%\GO.BAT
echo:ECHO:NET USE %%%%MapLetter%%%%\\%COMPUTERNAME%\%LinkName% ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:ECHO:echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Pulpit (SET Esc_LinkDest=%%%%ALLUSERSPROFILE%%%%\Pulpit\ ) %%>>%% %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:ECHO:echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Desktop  (SET Esc_LinkDest=%%%%ALLUSERSPROFILE%%%%\Desktop\ ) ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:ECHO:echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Pulpit\%LinkName%.lnk GOTO DLP1 ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:ECHO:echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Desktop\%LinkName%.lnk GOTO DLP1 ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:echo Set oWS = WScript.CreateObject("WScript.Shell") ^^> tmp.vbs ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:echo sLinkFile = "%%%%Esc_LinkDest%%%%%LinkName%.lnk" ^^>^^> tmp.vbs ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:echo Set oLink = oWS.CreateShortcut(sLinkFile) ^^>^^> tmp.vbs ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:echo oLink.TargetPath = "%%%%MapLetter%%%%\" ^^>^^> tmp.vbs ^>^>%%AutoStart%% >> %Esc_LinkTarget%\GO.BAT
echo:echo:echo oLink.Save ^^>^^> tmp.vbs ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:cscript //nologo .\tmp.vbs 6^>^^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:del .\tmp.vbs >^^^^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT
echo:echo:^:DLP1 ^>^> %%AutoStart%% >>%Esc_LinkTarget%\GO.BAT

...我希望go.bat有这样的东西......

IF EXIST %%ALLUSERSPROFILE%%\Pulpit (SET AutoStart="%%ALLUSERSPROFILE%%\Menu Start\Programy\Autostart\go2.bat" )
IF EXIST %%ALLUSERSPROFILE%%\Desktop  (SET AutoStart="%%ALLUSERSPROFILE%%\Microsoft\Windows\Start Menu\Programs\Startup\go2.bat" )
ECHO:REM DEKLARACJA LITERY MAPOWANEGO DYSKU > %%AutoStart%%
ECHO:SET MapLetter=T: >> %%AutoStart%%
ECHO:NET USE %%%%MapLetter%%%%\\%COMPUTERNAME%\%LinkName% >> %%AutoStart%%
echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Pulpit (SET Esc_LinkDest=%%%%ALLUSERSPROFILE%%%%\Pulpit\ ) >> %%AutoStart%%
echo:IF EXIST %%%%ALLUSERSPROFILE%%%%\Desktop  (SET Esc_LinkDest=%%ALLUSERSPROFILE%%\Desktop\ ) >> %%AutoStart%%
echo:IF EXIST %%ALLUSERSPROFILE%%\Pulpit\%LinkName%.lnk GOTO DLP1 >> %%AutoStart%%
echo:IF EXIST %%ALLUSERSPROFILE%%\Desktop\%LinkName%.lnk GOTO DLP1 >> %%AutoStart%%
echo:echo Set oWS = WScript.CreateObject("WScript.Shell") ^>^> tmp.vbs >> %%AutoStart%%
echo:echo sLinkFile = "%%Esc_LinkDest%%%%LinkName%%.lnk" ^>^> tmp.vbs >> %%AutoStart%%
echo:echo Set oLink = oWS.CreateShortcut(sLinkFile) ^>^> tmp.vbs >> %%AutoStart%%
echo:echo oLink.TargetPath = "%%MapLetter%%\" ^>^> tmp.vbs >> %%AutoStart%%
echo:echo oLink.Save ^>^> tmp.vbs >> %%AutoStart%%
echo:cscript //nologo .\tmp.vbs >> %%AutoStart%%
echo:del .\tmp.vbs >> %%AutoStart%%
echo:^:DLP1 >> %AutoStart%

和go2.bat有这个:

ECHO:REM DEKLARACJA LITERY MAPOWANEGO DYSKU
SET MapLetter=T:
NET USE %MapLetter%\\%COMPUTERNAME%\%LinkName%
IF EXIST %ALLUSERSPROFILE%\Pulpit (SET Esc_LinkDest=%ALLUSERSPROFILE%\Pulpit\ )
IF EXIST %ALLUSERSPROFILE%\Desktop  (SET Esc_LinkDest=%ALLUSERSPROFILE%\Desktop\ )
IF EXIST %ALLUSERSPROFILE%\Pulpit\%LinkName%.lnk GOTO DLP1
IF EXIST %ALLUSERSPROFILE%\Desktop\%LinkName%.lnk GOTO DLP1
echo Set oWS = WScript.CreateObject("WScript.Shell") >> tmp.vbs
echo sLinkFile = "%Esc_LinkDest%%LinkName%.lnk" >> tmp.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> tmp.vbs
echo oLink.TargetPath = "%%MapLetter%%\" >> tmp.vbs
echo oLink.Save >> tmp.vbs
cscript //nologo .\tmp.vbs
del .\tmp.vbs
:DLP1

你能帮我解决这个问题吗?我不知道怎么写第一个创建go2.bat

的人

2 个答案:

答案 0 :(得分:1)

如果您更喜欢解决方案而不必担心逃避字符:

@echo off
rem go.bat
echo hello, this is file nr. 1
echo it will create file nr. 2
for /f "delims=:" %%i in ('findstr /n /b "::DATA1" "%~df0"') do set datastart=%%i
more +%datastart% "%~df0" >go1.bat
set a=alpha
set b=beta
echo starting go1.bat:
call go1.bat
echo back to the root.
echo %a%,%b%,%c%,%ab%,%bc%
exit /b

::DATA1
rem go1.bat
echo    hello, this is file nr. 2
echo    it will create file nr. 3 
for /f "delims=:" %%i in ('findstr /n /b "::DATA2" "%~df0"') do set datastart=%%i
more +%datastart% "%~df0" >go2.bat
set c=gamma
set ab=%a%%b%
echo    starting go2.bat
call go2.bat
echo    back to level 1
echo    doing something more
echo    %a%,%b%,%c%,%ab%,%bc%
exit /b

::DATA2
rem go2.bat
echo        hello, this is file nr. 3
echo        I will do something.
set bc=%b%%c%
echo        %a%,%b%,%c%,%ab%,%bc%
exit /b

答案 1 :(得分:0)

您可以在Notepad ++中轻松进行此类转换,也可以使用任何其他实用程序进行转换,以便使用正则表达式进行搜索和替换。您只需搜索并替换以下内容:

replace "^" with "echo "
replace "\^" with "\^\^"
replace ">" with "^>"
replace "%" with "%%"
replace "$" with " >> go2.bat"

(替换顺序很重要。)

在此类操作中还有更多需要转义的字符,但在go2.bat的情况下,只有>%需要它。您应该咨询http://www.robvanderwoude.com/escapechars.php并尝试编写适当的正则表达式。

此外,我建议您在脚本开头添加一行,以删除go2.bat(如果已存在)。或者,您可以先将>>手动更改为>,以便始终截断现有的go2.bat

<强>更新

仅使用我为您创建的上述替换规则示例批处理脚本,该脚本创建其他批处理脚本并创建另一个批处理脚本。我已经使用上面的规则两次 - 首先为最后一个脚本创建生成器,然后创建生成器的生成器。在这里,检查:

echo echo ECHO:REM DEKLARACJA LITERY MAPOWANEGO DYSKU ^> drugi.bat > pierwszy.bat
echo echo SET MapLetter=T: ^>^> drugi.bat >> pierwszy.bat
echo echo NET USE %%%%MapLetter%%%%\\%%%%COMPUTERNAME%%%%\%%%%LinkName%%%% ^>^> drugi.bat >> pierwszy.bat
echo echo IF EXIST %%%%ALLUSERSPROFILE%%%%\Pulpit (SET Esc_LinkDest=%%%%ALLUSERSPROFILE%%%%\Pulpit\ ) ^>^> drugi.bat >> pierwszy.bat
echo echo IF EXIST %%%%ALLUSERSPROFILE%%%%\Desktop  (SET Esc_LinkDest=%%%%ALLUSERSPROFILE%%%%\Desktop\ ) ^>^> drugi.bat >> pierwszy.bat
echo echo IF EXIST %%%%ALLUSERSPROFILE%%%%\Pulpit\%%%%LinkName%%%%.lnk GOTO DLP1 ^>^> drugi.bat >> pierwszy.bat
echo echo IF EXIST %%%%ALLUSERSPROFILE%%%%\Desktop\%%%%LinkName%%%%.lnk GOTO DLP1 ^>^> drugi.bat >> pierwszy.bat
echo echo echo Set oWS = WScript.CreateObject("WScript.Shell") ^^^>^^^> tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo echo sLinkFile = "%%%%Esc_LinkDest%%%%%%%%LinkName%%%%.lnk" ^^^>^^^> tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo echo Set oLink = oWS.CreateShortcut(sLinkFile) ^^^>^^^> tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo echo oLink.TargetPath = "%%%%%%%%MapLetter%%%%%%%%\" ^^^>^^^> tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo echo oLink.Save ^^^>^^^> tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo cscript //nologo .\tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo del .\tmp.vbs ^>^> drugi.bat >> pierwszy.bat
echo echo :DLP1 ^>^> drugi.bat >> pierwszy.bat