批处理文件中的多参数循环

Multiple argument looping in batch files

我正在尝试定义一个批处理文件,它可以自动将多个 SVN URL 更新或检出到特定的文件路径中,它将用于设置新用户并使他们保持最新状态。在这一点上,这是一个通用的批处理文件问题,而不是 SVN 或 TortoiseSVN 问题。问题是如何将我多次复制和粘贴的内容放入一个循环中,我可以将两个参数输入其中。

该过程是重复的,并接受两个变量(A URL 和本地文件路径),因此它非常适合循环,但 URLs 和文件路径对于每个变量都是唯一的,所以我不得不复制并粘贴它们,这使得维护变得困难。
我在 FOR %%A IN ( x x x x ) DO START xxx 示例中使用了多个文件路径,这是我从堆栈溢出搜索中得到的,我在最后注释掉了它,这是从我决定我希望这是一个自动更新和自动结帐批次。我似乎无法让它适用于多个变量,因为那个例子只有一个。

目标是拥有一个自包含的批处理文件,我不必提供参数或拥有多个批处理文件。环境是Windows10.

与原始问题无关:此外,我 post 将此作为对其他 post 的感谢,我已经看到了这一步。我喜欢用于多线程的 "start" 命令。我无法弄清楚如何让 Tortoiseproc 为我做结帐,现在它只是打开一个 window 因此如果没有安装 svn.exe 则回显让用户点击确定。我对 .exe 检查的路径进行了硬编码,因为我没有时间尝试 "where" 命令,而且错误级别检查看起来很笨拙。我让这个批处理文件按原样工作,然后将其设为通用,如果有任何问题,我深表歉意。关于如何使语法突出显示正确显示在 post 中的任何评论(我读到它应该在标签中获取它但我尝试了其他语言说明符但它似乎没有接受它所以我会看看我点击 post) 后会发生什么。让我知道。

@echo off 
setlocal
REM This batch file can be used to perform the initial checkout if needed and update svn working copys automatically.
REM It automatically detects a lack of TortoiseSVN.  It has a workaround for if SVN commandline is not installed.
REM If at least TortoiseSVN is installed it automatically detects if the working copy does not exist and performs a checkout.
REM If at least TortoiseSVN is installed it automatically updates already existing working copies.
REM You can automate it via task scheduler.  Ex: Upon lock if network is connected and user is logged in; and/or daily in the morning.
REM TODO: The templates folder has trouble upon initial checkout cleaning out the old normal.dotm file because it already exists.  Probably add hard cleanup.

if not exist "C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe" (
    echo Install TortoiseSVN with command line option
    pause
    exit
) else (
    if not exist "C:\Program Files\TortoiseSVN\bin\svn.exe" (
    echo Proceeding without SVN commandline option.
    echo Hit ok when TortoiseSVN checkout windows open.
    pause
    )
)
REM if everything is good then proceed on!

REM Everyone
SET REPO1_URLDIR=https://REPO1
SET REPO1_PATHDIR=C:\Users\Public\Documents\REPO1

SET REPO2_URLDIR=https://REPO2
SET REPO2_PATHDIR=%appdata%\Microsoft\Templates\

REM designers:
SET REPO3_URLDIR=https://REPO3
SET REPO3_PATHDIR=C:\Users\Public\Documents\REPO3

SET REPO4_URLDIR=https://REPO4
SET REPO4_PATHDIR=C:\Users\Public\Documents\REPO4

REM Optional:
SET REPO5_URLDIR=https://REPO5
SET REPO5_PATHDIR=C:\Users\Public\Documents\REPO5

SET REPO6_URLDIR=https://REPO6
SET REPO6_PATHDIR=C:\Users\Public\Documents\REPO6

REM check if folder exists
if NOT exist "%REPO1_PATHDIR%" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO1_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" ( 
        START svn checkout %REPO1_URLDIR% %REPO1_PATHDIR%
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:%REPO1_PATHDIR% /url:"%REPO1_URLDIR%" 
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO1_PATHDIR% /closeonend:3 )

if NOT exist "%REPO2_PATHDIR%\blank.potx" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO2_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" (
        START svn checkout %REPO2_URLDIR% %REPO2_PATHDIR% 
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:%REPO2_PATHDIR% /url:"%REPO2_URLDIR%" 
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO2_PATHDIR% /closeonend:3 )

if NOT exist "%REPO3_PATHDIR%" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO3_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" ( 
        START svn checkout %REPO3_URLDIR% %REPO3_PATHDIR%
    ) else (
        START TortoiseProc.exe /command:checkout /path:%REPO3_PATHDIR% /url:"%REPO3_URLDIR%"
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO3_PATHDIR% /closeonend:3 )

if NOT exist "%REPO4_PATHDIR%" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO4_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" ( 
        START svn checkout %REPO4_URLDIR% %REPO4_PATHDIR%
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:%REPO4_PATHDIR% /url:"%REPO4_URLDIR%"
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO4_PATHDIR% /closeonend:3 )

if NOT exist "%REPO5_PATHDIR%" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO5_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" ( 
        START svn checkout %REPO5_URLDIR% %REPO5_PATHDIR%
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:%REPO5_PATHDIR% /url:"%REPO5_URLDIR%"
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO5_PATHDIR% /closeonend:3 )

if NOT exist "%REPO6_PATHDIR%" (
    rem file doesn't exist.  Create and checkout
    ECHO missing "%REPO6_PATHDIR%" beginning download
    if exist "C:\Program Files\TortoiseSVN\bin\svn.exe" ( 
        START svn checkout %REPO6_URLDIR% %REPO6_PATHDIR%
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:%REPO6_PATHDIR% /url:"%REPO6_URLDIR%"
    )
) else ( START TortoiseProc.exe /command:update /path:%REPO6_PATHDIR% /closeonend:3 )


REM update only:

REM FOR %%A IN (
REM "%REPO1_PATHDIR%"
REM "%REPO2_PATHDIR%"
REM "%REPO3_PATHDIR%"
REM "%REPO4_PATHDIR%"
REM "%REPO5_PATHDIR%"
REM "%REPO6_PATHDIR%"
REM ) DO START TortoiseProc.exe /command:update /path:%%A /closeonend:3

REM • The "FOR %%A" loop will contain, obviously, paths to the projects you want to update.
REM • The "START" bit means "START asynchronously, ie don't wait for end of previous task to launch next one" so that all the Update windows will pop up simultaneously.
REM • Use the "/closeonend:0" to test it first. Means, "don't close the Update window once it's done", so you can actually see what has been updated.
REM There you go. You can even put this .bat file in your STARTup folder to get things updated when you turn your computer on.
REM
REM To close the progress dialog at the end of a command automatically without using the permanent setting you can pass the /closeonend parameter.
REM • /closeonend:0 don't close the dialog automatically
REM • /closeonend:1 auto close if no errors
REM • /closeonend:2 auto close if no errors and conflicts
REM • /closeonend:3 auto close if no errors, conflicts and merges
REM To close the progress dialog for local operations if there were no errors or conflicts, pass the /closeforlocal parameter.

您重复的代码块具有以下形式:

if NOT exist _REPO_N_PATH_ (
    rem file doesn't exist.  Create and checkout
    ECHO missing _REPO_N_PATH_ beginning download
    if exist _SVN_PATH_ ( 
        START svn checkout _REPO_N_URL_ _REPO_N_PATH_
    ) else ( 
        START TortoiseProc.exe /command:checkout /path:_REPO_N_PATH_ /url:_REPO_N_URL_
    )
) else ( START TortoiseProc.exe /command:update /path:_REPO_N_PATH_ /closeonend:3 )

我数了三个参数:1) Repo 路径,2) SVN 可执行文件路径(虽然这可以保持不变)和 3) repo URL,还有一种特殊情况,其中特定文件name 仅用于测试其存在,因此为此添加第 4 个可选参数。

编写一个子程序并调用它:

@setlocal ENABLEEXTENSIONS
@rem @set prompt=$G

@rem You can add a search for svn later.
@set _SVN_Path="C:\Program Files\TortoiseSVN\bin\svn.exe"

@call :Action C:\Users\Public\Documents\REPO1 %_SVN_Path% https://REPO1
@call :Action "%appdata%\Microsoft\Templates" %_SVN_Path% https://REPO2 blank.potx
@call :Action "C:\Users\Public\Documents\REPO3" %_SVN_Path% https://REPO3
@rem Etc...
@exit /b 0

@REM Action takes three parameters:
@REM  %%1 is the repo path.
@REM  %%2 is the path to the SVN executable.
@REM  %%3 is the papth to the URI.
@REM  %%4 is optional file name to look for rather than just the directory.
:Action
@if NOT exist %1\%4 (
    @rem file doesn't exist.  Create and checkout
    @ECHO missing %1 beginning download
    @if exist %2 (
        @START svn checkout %3 %1
    ) else (
        @START TortoiseProc.exe /command:checkout /path:%1 /url:%3
    )
) else ( @START TortoiseProc.exe /command:update /path:%1 /closeonend:3 )

以上内容未经测试,但如果我有任何错误,应该不会太难调试。只需在测试时删除要在输出中看到的任何行前面的 @ 符号。