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 に答える