【问题标题】:Batch - IF EXIST statement not executing批处理 - IF EXIST 语句未执行
【发布时间】:2013-06-06 17:41:05
【问题描述】:

我正在创建一个批处理文件来自动执行一些备份。我已经退出 DOS / 批处理游戏好几年了,我已经很生疏了。我确定我的问题很小,但我似乎无法让我的 if 语句做任何事情。以下是违规行:

IF EXIST %dirname% (echo Directory already exists) ELSE (mkdir %dirname%)
::dirname in this particular case is: Backup 2013-06-06
::The directory does exist, so the echo statement should execute.

即使满足条件,我的 echo 语句也不会触发。在命令提示符中键入此行时可以正常工作,但不能从 .bat 中键入。有谁知道是什么问题?

谢谢!

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    你需要“引用名称”因为它包含一个空格

    IF EXIST "%dirname%" (echo Directory already exists) ELSE (mkdir "%dirname%")
    

    虽然

    mkdir "%dirname%" 2>nul
    

    也可以——它只是抑制STDERR(设备2)上的错误消息输出

    【讨论】:

    • 成功了!我知道这很简单,但由于某种原因,它正在逃避我。 (我确实知道 2>nul 位。我的 IF 语句最终会变得更加复杂,因为我现在又开始工作了)。谢谢!
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多