Kalen Brown
Kalen Brown

Reputation: 65

Multiple argument looping in batch files

I am trying to define a batch file that can automatically update or checkout multiple SVN URLs into specific file paths, it will be used to setup new users and keep them up to date. At this point, it is a generic batch file question and not a SVN or TortoiseSVN question. The question is how to make what I have copied and pasted multiple times into a loop that I can feed the two arguments into.

The procedure is repetitive and takes in two variables (A URL and a local file path), so it is ideal for a loop but the URLs and file paths are unique for each, so I had to copy and paste them down which makes maintenance difficult.
I had this working for multiple file paths in the FOR %%A IN ( x x x x ) DO START xxx example that I got from stack overflow searching and I have commented out at the end which is what this morphed from when I decided I wanted this to be an auto update and auto checkout batch. I can't seem to get it to work for multiple variables since that example just has one.

The goal is to have a self contained batch file that I don't have to feed arguments or have multiple batch files. Environment is Windows 10.

Unrelated to the original question: Also, I am posting this as a thank you to the other posts I have looked at to get this far. I like the "start" command for multithreading. I couldn't figure out how to get Tortoiseproc to just do the checkout for me, right now it just opens a window hence the echo to have the user hit ok if svn.exe is not installed. I hard coded the paths for the .exe checks because I didn't have time to try the "where" command and the error level check looked clunky. I got this batch file to work as it is and then made it generic, sorry if that broke anything. Any comments on how to get the syntax highlighting to show up in the post properly (I read that it should pick it up in the tags but I tried the other language specifier and it didn't seem to pick it up so I will see what happens after I hit post) let me know.

@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.

Upvotes: 0

Views: 313

Answers (1)

jwdonahue
jwdonahue

Reputation: 6659

Your repeated code blocks have the form:

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 )

I count three parameters: 1) Repo path, 2) SVN executable path (though this could be left constant) and the 3) repo URL, and there is the one special case where a specific file name is used just to test for its existence, so add a 4th optional parameter for that.

Write a subroutine and call it:

@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 )

The above is untested, but shouldn't be too difficult to debug if I got any of it wrong. Just remove the @ symbol in front of any line you want to see in the output while testing.

Upvotes: 1

Related Questions