本文介绍了在批处理文件中遇到嵌套 IF 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
)

我哪里出错了?

推荐答案

Never use :label nor :: label-like comment inside括在 () 括号中的命令块.证明:

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
)

输出:

==>D:atlabels.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

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

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%".

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

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

  • 如果输入:使用检查它如果不存在%svnroot_temp%\%direct%"转到:top before通过setsvnroot_temp"改变它=%svnroot_temp%\%direct%"
  • 如果输出:使用 MD "%svnroot_temp%" 2>NUL after 更改它来创建它.
  • 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.

这篇关于在批处理文件中遇到嵌套 IF 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:27