首页 > 解决方案 > 即使为真,批处理文件也会忽略 If 语句

问题描述

在某个批处理文件的某个地方,我有一段代码可以检查目录是否存在。如果没有,请询​​问用户该怎么做。现在奇怪的是,当目录存在时,我仍然会收到“你想创建目录吗?”的问题。在 if 语句中。

我在这里想念什么?

cls
@echo off

SET PanterPath="W:\Windows\Panther\"

if NOT exist %PanterPath% (
    echo Directory %PanterPath% not found! Cannot copy and apply unattend file.
    SET /P var=Do you want to create the directory? (Y/N)
    if /I "%var%" EQU "Y" (
        echo Creating directory %PanterPath% ...
        mkdir %PanterPath%        
    ) else (
        echo Directory %PanterPath% not created so script cannot continue. The image deployment failed!    
        pause    
        exit
    )    
)
pause
pause

编辑当我向If Existsthen 中添加 else 语句时,输出为:

Directory "W:\Windows\Panther" found

Directory "W:\Windows\Panther" not created so script cannot continue. The image deployment failed

Press the any key...

代码:

SET PanterPath="W:\Windows\Panther"

If Exist "%PanterPath%\" (
    echo Directory %PanterPath% found!
) else (
    echo Directory %PanterPath% not found! Cannot copy and apply unattend file.
    SET /P var=Do you want to create the directory? (Y/N)
    if /I "%var%" == "Y" (
        echo Creating directory %PanterPath% ...
        mkdir %PanterPath%        
    ) else (
        echo Directory %PanterPath% not created so script cannot continue. The image deployment failed!    
        pause    
        exit
    )    
)
pause
pause

标签: windowsbatch-filecmdnested-ifdelayedvariableexpansion

解决方案


推荐阅读