0

I am making a batch file for checking out a project from SVN. I ask the user to enter the directory and when you reach the directory you want, you type checkout and it checks out that project directory. However, I am having some trouble with the code below. Please help.

if /i %choice%==1  ( 
cls
svn ls %svnroot_temp%
:top
set /p direct=Enter directory:
if %direct%=checkout( goto :checkout_area )
set svnroot_temp= %svnroot_temp%/%direct%
svn ls %svnroot_temp%
goto :top
)

Where am I going wrong here?

Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • 1
    Not sure you can add a checkpoint `:top` in the `if` statement. And also, add `Setlocal EnableDelayedExpansion` and replace all `%` with `!` in the code block. – Happy Face Aug 21 '15 at 18:07
  • :top is actually working but when I add the new if clause the block fails , since the batch file exits. I tried replacing % with ! too after adding the setlocal value. It works now !! thanks a lot!. – Gaurav.dwrg Aug 21 '15 at 18:19

1 Answers1

3

Never use :label nor :: label-like comment inside a command block enclosed in () parentheses. Proof:

@ECHO %1>NUL
if "" == "" (
    @echo a simple echo, no comments
)
if ""=="" (
  @echo a rem comment follows this echo command
  rem comment
  @echo a rem comment precedes this echo command
)
if ""=="" (
  @echo a label-like comment follows this echo command
  :: comment
  @echo a label-like comment precedes this echo command
)
if ""=="" (
  @echo a label follows this echo command
  :label
  @echo a label precedes this echo command
)

Output:

==>D:\bat\labels.bat OFF
a simple echo, no comments
a rem comment follows this echo command
a rem comment precedes this echo command
a label-like comment follows this echo command
'@echo' is not recognized as an internal or external command,
operable program or batch file.
a label follows this echo command
'@echo' is not recognized as an internal or external command,
operable program or batch file.

==>

Next code snippet should work as expected, if I can understand your aim:

SETLOCAL enableextensions

rem (set `svnroot_temp` and `choice` variables here)

if /i "%choice%"=="1"  ( 
    cls
    svn ls %svnroot_temp%
    call :top
)
goto :eof

:top
set /p direct=Enter directory:
if /I "%direct%"=="checkout" goto :checkout_area
set "svnroot_temp=%svnroot_temp%\%direct%"
svn ls %svnroot_temp%
goto :top

:checkout_area

Note that both compared expressions in if /I "%direct%"=="checkout" goto :checkout_area are enclosed in double quotes as any user input could contain a space or even could stay empty.
Not sure about quoting in svn ls "%svnroot_temp%".

Not sure whether "%svnroot_temp%" is an input or output directory for svn ls command:

  • in case input: check it using if not exist "%svnroot_temp%\%direct%\" goto :top before changing it by set "svnroot_temp=%svnroot_temp%\%direct%"
  • in case output: create it using MD "%svnroot_temp%" 2>NUL after changing it.
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • As a general rule it's useful to prevent unwanted behaviour, but you could use it when you know that labels inside blocks always spawns over two lines and the second line has some *special* rules – jeb Sep 15 '15 at 21:40