我花了幾天的時間試圖讓這個批處理腳本能夠正常工作,但它似乎不能正常工作。它似乎只是在它提示我設置一個變量並設置它之後做任何想要的事情。如果命令正確,我的批處理腳本不會使用
例如,我可能會輸入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
我該怎麼辦可能是做錯了什麼?
謝謝
P.S.我通過打開CMD並在那裏多次運行批處理腳本來測試這一點。 (但是當它雙擊時它也不能正常工作)
我很抱歉,但我不知道該怎麼來形容它。英語不是我的母語。 – ditheredtransparency
一旦你輸入了第一個IF塊,所有的變量都需要通過延遲擴展來引用。 – Squashman
@abelenky:這個怎麼樣?:_「[當前目錄不存在](http://stackoverflow.com/questions/41310409/current-directory-does-not-exist-batch-files)」_ – Aacini