1

我有一个批处理文件,我在其中创建了许多这样的快捷方式:

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 循环中,我想不出在每次迭代中用新值覆盖变量的好方法。

你有什么建议?

4

1 回答 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%
于 2012-06-08T16:06:42.993 回答