This batch script terminates when %CHECKCONTINUE% is given a null value by not inputting anything on line 13 (SET /p CHECKCONTINUE=Okay to continue? (y/n):), why is this?

@ECHO OFF
SETLOCAL
TITLE Registry restore script
REM Restores registry settings and disables the cloud

SET %CHECKCONTINUE%=

:listaction
ECHO I'm about to...
ECHO 1.) Remove the registry data that specifies settings for TF2
ECHO 2.) Forcibly disable Steam Cloud.
ECHO.
SET /p CHECKCONTINUE=Okay to continue? (y/n): 

REM No?
IF %CHECKCONTINUE%==n GOTO exit
IF %CHECKCONTINUE%==no GOTO exit

REM Yes?
IF %CHECKCONTINUE%==y GOTO start
IF %CHECKCONTINUE%==yes GOTO start

REM Did they put something else?
IF DEFINED %CHECKCONTINUE% GOTO loop-notvalid

REM Did they not put anything at all?
IF NOT DEFINED %CHECKCONTINUE% GOTO loop-noreply

:start
REM Delete application specific data
REG DELETE HKEY_CURRENT_USER\Software\Valve\Source\tf\Settings /f
REG DELETE HKEY_CURRENT_USER\Software\Valve\Steam\Apps\440 /f

REM Disable Steam Cloud for TF2
REG ADD HKEY_CURRENT_USER\Software\Valve\Steam\Apps\440 /v Cloud /t REG_DWORD /d "0x0" /f

:exit
ENDLOCAL
EXIT

:loop-notvalid
ECHO.
ECHO That's not a valid reply. Try again.
ECHO.
SET %CHECKCONTINUE%=
GOTO listaction

:loop-noreply
ECHO.
ECHO You must enter a reply.
ECHO.
SET %CHECKCONTINUE%=
GOTO listaction
link|improve this question

What's with people writing batch scripts in all-uppercase? (Coding style question, I know, but...) – grawity Dec 23 '10 at 13:29
1  
Habit from writing shell scripts. – Matthieu Cartier Dec 23 '10 at 13:34
feedback

1 Answer

up vote 3 down vote accepted

First, on multiple lines you have:

SET %CHECKCONTINUE%=

This doesn't modify CHECKCONTINUE but uses its value as the variable name.

Change it to:

SET CHECKCONTINUE=

It would also be better if you moved it immediately above set /p ... -- this way you would only need it once.


if defined also only takes a variable name, so instead of

IF DEFINED %CHECKCONTINUE% GOTO loop-notvalid

you should use:

IF DEFINED CHECKCONTINUE GOTO loop-notvalid

Same applies to this line:

IF NOT DEFINED %CHECKCONTINUE% GOTO loop-noreply

However, it can be shortened to:

GOTO loop-noreply

If the variable were defined, the execution would never reach this line anyway (if defined ... above)


This is how I would have written it:

@echo off & setlocal
title Registry restore script
:: Restores registry settings and disables the Cloud

:menu
echo I'm about to...
echo 1) Remove the registry data that specifies settings for TF2
echo 2) Forcibly disable Steam Cloud.
echo.
set check=
set /p check=Okay to continue? (y/n)
:: /i means case-insensitive comparison
if /i %check%==y goto :start
if /i %check%==yes goto :start
if /i %check%==n goto :EOF
if /i %check%==no goto :EOF
:: On empty response, pick the safest option as default
if not defined check goto :EOF

goto :loop-invalid

:start
:: Delete application specific data
reg delete HKCU\Software\Valve\Source\tf\Settings /f
reg delete HKCU\Software\Valve\Steam\Apps\440 /f

:: Disable Steam Cloud for TF2
reg add HKCU\Software\Valve\Steam\Apps\440 /v Cloud /t REG_DWORD /d "0x0" /f

:loop-invalid
echo.
echo Not a valid answer.
goto :menu
link|improve this answer
If the script were run from inside an interactive Command Prompt shell, a plain exit would terminate the shell too, not just the script. Within a script, one should use either goto :EOF or exit /b. (endlocal is not necessary.) – grawity Dec 23 '10 at 13:29
It still terminates if you press enter with no input on set /p check=Okay to continue? (y/n) :( – Matthieu Cartier Dec 23 '10 at 13:33
@neurolysis: If you are talking about the rewritten version from my post, it's because I wrote it to. IMO, since there is a sensible default option ("no, don't restore"), pressing Enter should just choose that option. If you disagree, change to if not defined check goto :loop-noreply. – grawity Dec 23 '10 at 13:37
Well, that's the issue at hand. Even with if not defined check goto :loop-noreply as a check, it terminates... – Matthieu Cartier Dec 23 '10 at 14:14
@neurolysis: ...did you (re)add a :loop-noreply section? Also, try moving the not defined check immediately after set /p. And run everything from within a Command Prompt window, error messages are helpful. – grawity Dec 23 '10 at 14:56
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

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