Anthony Carr
Anthony Carr

Reputation: 1

Can some one tell me what is wrong with this bat script?

It's a simple bat script that should checkout some folders from svn . I'm not that up on bat scripting, there seems to be no consistency on how variables are referenced. For instance the variable "branchV" does not get appended it is seen as '""', but if I echo it I see the user input.

set "DB_DIRECTORIES=AuditUser-db CarrierProcessingRules-db   iDetectDB-db iRisk-db     WarningsIndex-db"
set "SVNBASEURL=http://XX.XX.XX.XX:7777/svn/YYY"
set BASELOCALDIRECTORY="C:"
@echo on


@cls


@echo Check out DB directories from?
@echo 
@echo 1. Trunk
@echo 2. Branch
@echo
@echo
@set OPTIONSELECTED=
@set /P OPTIONSELECTED=SELECT OPTION:%=%


if "%OPTIONSELECTED%" == "1" (


set SVNURL="%SVNBASEURL%/trunk"
set BRANCHV="BCSTrunk"

) ELSE IF %OPTIONSELECTED% == 2 (

 @echo
 @echo
 @echo
 @echo PLEASE ENTER THE BRANCH VERSION YOU WISH TO CHECKOUT
 @echo
 @echo
 @set branchV=
 @set /P branchV=ENTER VERSION:%=%
 @echo

 set SVNURL="%SVNBASEURL%"/branches/"%branchV%"

 ) ELSE (
@echo Invalid option

 )


for %%i in (%DB_DIRECTORIES%) do (

 set PATHTOUSE="%BASELOCALDIRECTORY%"/"%branchV%"/%%i
 set NEWSVNURL="%SVNURL%"/%%i
 REM Intended to remove all inverted commas , which were causing an issue in svn
 set PATHTOUSE="%PATHTOUSE:=%"
 set NEWSVNURL=%NEWSVNURL:=%


  TortoiseProc.exe /command:checkout /path:%PATHTOUSE% /url:%NEWSVNURL%  /closeonend:1

)

Upvotes: 0

Views: 44

Answers (1)

MC ND
MC ND

Reputation: 70923

In batch scripts, when a line or a block of code (the code enclosed in parenthesis) is reached, all variable reads are replaced with the value they have before starting to execute that line or block. So, if you change a variable inside a block, you can not retrieve this value inside the same block. There are no reads of the variable value, they were replaced with its values.

To correctly retrieve the changed value inside the same block, it is necessary to indicate to cmd that the read operations should be delayed until the line is executed. To do this, two steps are necessary. First is enable this option

setlocal enabledelayedexpansion

When it is enabled, variables that should be read delayed need the sintax !var! instead of %var%

Upvotes: 2

Related Questions