Page 1 of 1

Batch Menus not setting variables

Posted: 19 Apr 2013 14:08
by wingnut0420
Try this on for size. First run through will not set the variable "wtf"
have it pausing so you can see example
but second run will set it

Also, try answering y to the first prompt, then just hitting enter at the next
seems like the first prompt will set it but not able to read it

WUT?????



Code: Select all

set prdico=Y
set WTF=

:WIconprompt
IF "%prdico%"=="Y" (
   Echo ==================================================
   
   Echo   Prod icon detected, would you like to install
   Echo  the "Schoolhouse" Icon for Non Production [Y/N]
   Echo ==================================================
   SET /P WTF=Please select [Y/N]
   IF "%WTF%"=="Y" (
         Set Return=END
         GOTO WIiconNP
         )
   IF "%WTF%"=="y" (
         Set Return=End
         GOTO WIiconNP
         )
   IF "%WTF%"=="N" GOTO END
   IF "%WTF%"=="n" GOTO END
   echo WTF= %WTF%
   echo oops
   echo ===============================
   echo Please select [Y/N]
   echo ===============================
   pause
   GOTO WIconprompt
)

:WIiconNP
echo works
pause


:End
echo end
pause

Re: Batch Menus not setting variables

Posted: 19 Apr 2013 14:15
by wingnut0420
Better example, cleaned up a bit

Try pressing Y to first prompt....then hitting enter second time


Code: Select all

@echo off
set prdico=Y
set WTF=

:WIconprompt
IF "%prdico%"=="Y" (
   Echo ==================================================
   Echo   Prod icon detected, would you like to install
   Echo  the "Schoolhouse" Icon for Non Production [Y/N]
   Echo ==================================================
   SET /P WTF=Please select [Y/N]
   IF "%WTF%"=="Y" (
         Set Return=END
         GOTO WIiconNP
         )
   IF "%WTF%"=="y" (
         Set Return=End
         GOTO WIiconNP
         )
   IF "%WTF%"=="N" GOTO END
   IF "%WTF%"=="n" GOTO END
   echo WTF= %WTF%
   echo oops
   echo ===============================
   echo Please select [Y/N]
   echo ===============================
   pause
   GOTO WIconprompt
)
GOTO end


:WIiconNP
echo works
pause
exit


:End
echo end
pause

Re: Batch Menus not setting variables

Posted: 19 Apr 2013 15:35
by Endoro

Code: Select all

IF "%prdico%"=="Y" (


Normal %variables% can't change their value inside a code block.
You should use delayed expansion and !variables!.

Re: Batch Menus not setting variables

Posted: 20 Apr 2013 04:59
by Ranguna173

Code: Select all

@echo off &setlocal enableDelayedExpansion
set prdico=Y

:WIconprompt
cls

IF /i "%prdico%" neq "Y" goto end

Echo ==================================================
Echo   Prod icon detected, would you like to install
Echo  the "Schoolhouse" Icon for Non Production [Y/N]
Echo ==================================================
SET /P WTF=Please select [Y/N]:
if not defined WTF goto wiconprompt
IF /i "%WTF%"=="Y" (
     Set Return=END
     GOTO WIiconNP
    )
IF /i "%WTF%"=="N" GOTO END
cls
echo WTF= %WTF%
echo oops
echo ===============================
echo Please select [Y/N]
echo ===============================
pause
GOTO WIconprompt



:WIiconNP
echo works
pause
exit


:End
echo end
pause


Try this one.

Code: Select all

IF /i "%prdico%" neq "Y" goto end

By doing this the batch will skip the code instead of adding it inside the if, this way you can change variables.

I added "/i" after the "if" it'll set the command to insensitive:

Code: Select all

if /i %var% (command)

%var% can be either "Y" or "y"

I also added "cls" to make it a little more clean.

Re: Batch Menus not setting variables

Posted: 22 Apr 2013 07:47
by wingnut0420
Thank for this, it helped quite a bit!