2017-03-03 5 views
0

このバッチスクリプトを動作させるのに数日を費やしましたが、正しく動作していないようです。それは私に変数を設定するように促した後、それを設定した後に必要なことをやっているようです。コマンドを正しく実行するとバッチスクリプトが使用されません

たとえば、存在していないと思われる場合はnと入力してください。そうすればスクリプトを終了するだけです。しかし、私がそれを開いて前と同じことを言い、nと入力すると、ちょうど私がyとタイプしたかのように、DeleteCalcにジャンプするかもしれません。私はおそらく間違って何をしていることができ

@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

Wonderful!どうもありがとうございます!魅力的な作品:) – ditheredtransparency

関連する問題