批处理文件将忽略If语句,即使为true

时间:2019-05-29 07:31:32

标签: windows batch-file cmd nested-if delayedvariableexpansion

在某个批处理文件中,我有一段代码检查目录是否存在。如果不是,则询问用户该怎么做。现在奇怪的是,当目录存在时,我仍然会问“您要创建目录吗?”在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 Exists添加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

0 个答案:

没有答案