嵌套IF(IF(...)ELSE(..))的批量语句 [英] Nested IF ( IF ( ... ) ELSE( .. ) ) statement in batch

查看:138
本文介绍了嵌套IF(IF(...)ELSE(..))的批量语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写嵌套在另一个如果语句中的 IF ELSE 语句。下面是我有:

I'm trying to write an IF ELSE statement nested inside another IF statement. Here's what I have:

IF %dirdive%==1 ( 
    IF DEFINED log (
        ECHO %DATE%, %TIME% >> %log%
        FOR /R %root1% %%G IN (.) DO (
            SET _G=%%G
            CALL :TESTEVERYTHING !_G:~0,-1! %root1% %root2% %log%
        )
        GOTO :end
    ) ELSE ( 
        ECHO %DATE%, %TIME%
        FOR /R %root1% %%G IN (.) DO (
            SET _G=%%G
            CALL :TESTEVERYTHINGnolog !_G:~0,-1! %root1% %root2%
        )
        GOTO :end
    )
)

日志没有定义,我得到:

The syntax of the command is incorrect.
ECHO Wed 07/18/2012, 15:50:12.34 >>

Aaaand我不知所措。我试着用括号玩。我搬到了最后一个)上输出到同一行面前的一个,这是行不通的。关键是,的它正常工作时日志定义的。这似乎是正确的之后或在打破IF%dirdive%== 1 ,因为它不会得到紧接着插入一个echo命令。

Aaaand I'm at a loss. I've tried playing with the parenthesis. I've moved the last ) up onto the same line as the one before it and it doesn't work. The thing is, it works fine when log is defined. It seems to break right after or at IF %dirdive%==1, as it won't get to an echo command inserted right after that.

推荐答案

你的问题的根源是,即使IF语句的一个分支,不执行,但仍必须具有有效的语法。

The source of your problem is that even if a branch of an IF statement does not execute, it still must have valid syntax.

日志没有定义,那么下面的行

When log is not defined, then the following line

ECHO %DATE%, %TIME% >> %log%

拓展到以下时,日志未定义

ECHO someDate, someTime >>

有是重定向后没有文件名,这会导致一个语法错误。

There is no file name after the redirection, which results in a syntax error.

只要你的登录变量尚未与封闭引号(当它被定义即是),则只需更改行,如下所示应该修复它定义为:

As long as your log variable is not already defined with enclosing quotes (when it is defined that is), then simply changing the line as follows should fix it:

ECHO %DATE%, %TIME% >> "%log%"

这行扩展到以下时,登录未定义

That line expands to the following when log is undefined

ECHO someDate, someTime >> ""

这是有效的语法。这将失败,该系统找不到指定的路径如果执行错误,但它不会执行,因为日志是未定义: - )

Which is valid syntax. It will fail with a "The system cannot find the path specified" error if it is executed, but it won't execute because log is undefined :-)

修改

也许是更好的解决方案是定义一个新的变量,包括当且仅当日志定义的值重定向操作。然后,你甚至不需要你的大IF语句和code更容易维护。

Perhaps a better solution is to define a new variable that includes the redirection operator in the value if and only if log is defined. Then you don't even need your big IF statement and the code is easier to maintain.

SET "redirect="
IF DEFINED log SET "redirect=>>!log!"
IF %dirdive%==1 (
  ECHO %DATE%, %TIME% %redirect%
  FOR /R %root1% %%G IN (.) DO (
    SET _G=%%G
    CALL :TESTEVERYTHING !_G:~0,-1! %root1% %root2% %log%
  )
  GOTO :end
)

请注意,正常的扩张%重定向%必须在ECHO语句中使用。延迟扩展!重定向!,因为之前延迟扩展命令解析器的重定向阶段出现将无法工作。

Note that normal expansion %redirect% must be used in the ECHO statement. Delayed expansion !redirect! will not work because the redirection phase of the command parser occurs prior to delayed expansion.

这篇关于嵌套IF(IF(...)ELSE(..))的批量语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆