I have following batch file code:
@echo off
SET INSTALL_PATH=c:\program files\
:ask_again
if exist "%INSTALL_PATH%" (
SET /P PATH_EXISTS_ANSWER=Path exists, overwrite?[y/n/default:n]
if not defined PATH_EXISTS_ANSWER (
echo You chose default action^(N^). Try another installation path.
echo.
goto default
)
if /I "%PATH_EXISTS_ANSWER%"=="n" (
echo You chose not to use existing folder. Try another installation path.
echo.
goto noc
)
if /I "%PATH_EXISTS_ANSWER%"=="y" (
echo You chose to overwrite existing folder. Existing files will be overwritten.
echo.
goto yesc
)
echo Please choose Y or N
echo.
goto ask_again
)
:yesc
echo you said yes
goto end
:default
echo you said default
goto end
:noc
echo you said no
goto end
:end
And it’s OK when i choose default action just pressing Enter. But when i use N or Y key, value of PATH_EXISTS_ANSWER seems to be undefined, script goes for another loop and then, no matter what i answer, script somewhy uses previous answer.
For example if i answer Y script asks again and if i choose N it types “you said yes”.
What am i doing wrong?
You need to put the following after
@echo off:Then, when you refer to
PATH_EXISTS_ANSWER, instead of enclosing it in percent marks, (%) enclose it in exclamation marks. (!) like this:!PATH_EXISTS_ANSWER!I tested it, and it works.