Having trouble with nested IF in a batch file [duplicate]
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?
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 byset "svnroot_temp=%svnroot_temp%\%direct%"
- in case output: create it using
MD "%svnroot_temp%" 2>NUL
after changing it.