我有一个批处理文件,我在这里创建了许多这样的快捷方式:
SET VBFILE=createshortcut.vbs
SET INSTALLPATH=%userprofile%\desktop
SET NETWORKPATH=I:\Program Files\app\Exec
SET TARGET_A=%NETWORKPATH%\myapp-CS.exe
SET LNKNAME_A=The name of the app CS
FOR /F "tokens=1* delims=;" %%B IN ("Set oWS = WScript.CreateObject("WScript.Shell")") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("sLinkFile = "%INSTALLPATH%\%LNKNAME_A%.lnk"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("Set oLink = oWS.CreateShortcut(sLinkFile)") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.TargetPath = "%TARGET_A%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.Arguments = """) DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.WorkingDirectory = "%NETWORKPATH%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.Save") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
CSCRIPT %INSTALLPATH%\%VBFILE%
DEL %INSTALLPATH%\%VBFILE%
SET TARGET_B=%NETWORKPATH%\myapp-BD.exe
SET LNKNAME_B=The name of the app BD
FOR /F "tokens=1* delims=;" %%B IN ("Set oWS = WScript.CreateObject("WScript.Shell")") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("sLinkFile = "%INSTALLPATH%\%LNKNAME_A%.lnk"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("Set oLink = oWS.CreateShortcut(sLinkFile)") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.TargetPath = "%TARGET_B%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.Arguments = """) DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.WorkingDirectory = "%NETWORKPATH%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.Save") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
CSCRIPT %INSTALLPATH%\%VBFILE%
DEL %INSTALLPATH%\%VBFILE%
有没有办法减少重复代码并将其放在GOTO语句或for循环中?
使用GOTO,我无法想出一种方法可以返回到GOTO被调用的地方。在for循环中,我无法找到在每次迭代中用新值覆盖变量的好方法。
你会建议什么?
答案 0 :(得分:2)
您需要使用功能:请查看此链接以获取更多信息Batch Functions
答案 1 :(得分:2)
您可以使用call
代替goto
,它会在功能完成后自动返回到调用代码。
@echo off
echo Starting batch file
call :RepeatFunction
echo Finished
:RepeatFunction
FOR /F "tokens=1* delims=;" %%B IN ("Set oWS = WScript.CreateObject("WScript.Shell")") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("sLinkFile = "%INSTALLPATH%\%LNKNAME_A%.lnk"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN ("Set oLink = oWS.CreateShortcut(sLinkFile)") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.TargetPath = "%TARGET_A%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.Arguments = """) DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" ' oLink.WorkingDirectory = "%NETWORKPATH%"") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%
FOR /F "tokens=1* delims=;" %%B IN (" oLink.Save") DO ECHO %%B>>%INSTALLPATH%\%VBFILE%