2017-03-03 27 views
0

我花了幾天的時間試圖讓這個批處理腳本能夠正常工作,但它似乎不能正常工作。它似乎只是在它提示我設置一個變量並設置它之後做任何想要的事情。如果命令正確,我的批處理腳本不會使用

例如,我可能會輸入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並在那裏多次運行批處理腳本來測試這一點。 (但是當它雙擊時它也不能正常工作)

+0

我很抱歉,但我不知道該怎麼來形容它。英語不是我的母語。 – ditheredtransparency

+0

一旦你輸入了第一個IF塊,所有的變量都需要通過延遲擴展來引用。 – Squashman

+0

@abelenky:這個怎麼樣?:_「[當前目錄不存在](http://stackoverflow.com/questions/41310409/current-directory-does-not-exist-batch-files)」_ – Aacini

回答

2

如果重新構建腳本,則不需要擴展If塊,因此不需要EnableDelayedExpansion。此外,如果您使用Choice,則無需執行所有回覆驗證。

例子:

@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 
+0

太棒了!非常感謝!奇蹟般有效 :) – ditheredtransparency