在批处理文件中遇到嵌套IF问题

时间:2015-08-21 18:01:16

标签: batch-file

我正在制作一个批处理文件,用于从SVN签出一个项目。 我要求用户输入目录,当您到达所需目录时,键入checkout并检出该项目目录。但是,我在使用下面的代码时遇到了一些问题。请帮忙。

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
)

我在哪里错了?

1 个答案:

答案 0 :(得分:3)

从不:label括号内的命令块中使用:: label-like comment()证明

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

<强>输出

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

==>

如果我能理解您的目标,下一个代码段应该按预期工作:

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

请注意,if /I "%direct%"=="checkout" goto :checkout_area中的两个比较表达式都用双引号括起来,因为任何用户输入都可以包含空格,甚至可以保持为空。
不确定引用svn ls "%svnroot_temp%"

不确定"%svnroot_temp%"是否是svn ls命令的输入或输出目录:

  • 如果输入:请先使用if not exist "%svnroot_temp%\%direct%\" goto :top 进行检查更改set "svnroot_temp=%svnroot_temp%\%direct%"
  • 以防输出:更改后使用MD "%svnroot_temp%" 2>NUL 创建。