Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 我的批处理脚本没有';不能正确使用if命令_Batch File_If Statement - Fatal编程技术网

Batch file 我的批处理脚本没有';不能正确使用if命令

Batch file 我的批处理脚本没有';不能正确使用if命令,batch-file,if-statement,Batch File,If Statement,我花了几天时间试图让这个批处理脚本工作,但它似乎不能正常工作。在它提示我设置一个变量并进行设置之后,它似乎可以做任何它想做的事情 例如,当它说它似乎不存在时,我可能会输入n,它会像应该的那样结束脚本。但是,如果我重新打开它,它显示的内容与以前相同,并且我再次输入n,它可能会跳转到:DeleteCalc,就像我键入了y 这是我的剧本: @echo off :Begin color fc title My script cls if not exist "C:\calc.exe" ( ech

我花了几天时间试图让这个批处理脚本工作,但它似乎不能正常工作。在它提示我设置一个变量并进行设置之后,它似乎可以做任何它想做的事情

例如,当它说它似乎不存在时,我可能会输入
n
,它会像应该的那样结束脚本。但是,如果我重新打开它,它显示的内容与以前相同,并且我再次输入
n
,它可能会跳转到:DeleteCalc,就像我键入了
y

这是我的剧本:

@echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
    echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
    set "calcnotexist="
    set /p "calcnotexist="

    ::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
    setlocal EnableDelayedExpansion
    if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
    endlocal & if "%calcnotexist%"=="" (
        echo ERROR - Quotes cannot be entered.
        pause
        goto Begin
    )

    if /i "%calcnotexist%"=="Y" (
        echo.
        goto DeleteCalc
    )
    if /i "%calcnotexist%"=="Yes" (
        echo.
        goto DeleteCalc
    )
    if /i "%calcnotexist%"=="N" goto End
    if /i "%calcnotexist%"=="No" goto End
    echo ERROR - Unrecognized input
    pause
    goto Begin
)

:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="

::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
    echo ERROR - Quotes cannot be entered.
    pause
    goto calcDoesExist
)

if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist

:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End

:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End

:End
exit /b
我可能做错了什么

谢谢


另外,我通过打开CMD并在其中多次运行批处理脚本来测试这一点。(但双击它也不能正常工作)

如果重新构造脚本,则不需要扩展
If
块,因此不需要启用
延迟扩展。此外,如果您使用
选项
,则不必对响应进行所有验证

例如:

@Echo Off
Title My script
Color FC

:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End

:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End

:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End

:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.

:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

如果重新构造脚本,则无需使用扩展的
If
块,因此无需启用
延迟扩展。此外,如果您使用
选项
,则不必对响应进行所有验证

例如:

@Echo Off
Title My script
Color FC

:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End

:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End

:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End

:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.

:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

对不起,我不知道该怎么形容它。英语不是我的母语。一旦你输入了第一个IF块,所有变量都需要延迟扩展引用。@abelenky:这一个呢?:“注意:user@abelenky posted:“它似乎想做什么就做什么,这是我读到的关于错误的最佳描述。”作为这个问题下的第一条评论,但后来被删除了。我添加此注释是因为如果没有删除的注释,我以前的注释似乎没有任何意义…对不起,但我不知道如何描述它。英语不是我的母语。一旦你输入了第一个IF块,所有变量都需要延迟扩展引用。@abelenky:这一个呢?:“注意:user@abelenky posted:“它似乎想做什么就做什么,这是我读到的关于错误的最佳描述。”作为这个问题下的第一条评论,但后来被删除了。我之所以加上这张便条,是因为如果没有删除的评论,我以前的评论似乎毫无意义……太棒了!非常感谢你!工作就像一个魅力:)太棒了!非常感谢你!工作就像一个符咒:)