0

I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.

For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.

Here's my script:

@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

What could I possibly be doing wrong?

Thanks

P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)

4
  • I'm sorry, but I don't know how else to describe it. English is not my native language. Commented Mar 3, 2017 at 5:09
  • Once you entered that first IF block, all your variables need to be referenced with delayed expansion.
    – Squashman
    Commented Mar 3, 2017 at 5:29
  • @abelenky: What about this one?: "Current Directory does not exist"
    – Aacini
    Commented Mar 3, 2017 at 5:33
  • Note: user @abelenky posted: "It seems to just do whatever it wants is the best description about an error I read." as the first comment under this question, but it was later deleted. I am adding this note because my previous comment seems to make no sense without the deleted comment...
    – Aacini
    Commented Mar 4, 2017 at 19:56

1 Answer 1

2

If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.

Example:

@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
1
  • Wonderful! Thank you so much! Works like a charm :) Commented Mar 3, 2017 at 19:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.