have to select it twice?

  • Thread starter Thread starter Mr Novice
  • Start date Start date
M

Mr Novice

Consider the following example...

1 set /p findingnemo=(A / B)
2
3 :baltimore
4 if /i "%findingnemo%"=="a" (
5 cls
6 echo You selected option A.
7 echo.
8 set /p toystory=Is this correct? (Y /N^)
9 if /i "%toystory%"=="y" (
10 set peterpan=option_a
11 goto washingtondc
12 )
13 if /i "%toystory%"=="n" goto cleveland
14 goto baltimore
15 )


The script is asking you to make a selection. After making
your selection it will ask you to confirm it. If you
select either Y or N for the confirmation it will take you
back to line 3 as if you had selected an incorrect option.
The second time you answer the confirmation question the
script works successfully. Can anybody explain this?

Thanks,

Chad
 
Mr said:
Consider the following example...

1 set /p findingnemo=(A / B)
2
3 :baltimore
4 if /i "%findingnemo%"=="a" (
5 cls
6 echo You selected option A.
7 echo.
8 set /p toystory=Is this correct? (Y /N^)
9 if /i "%toystory%"=="y" (
10 set peterpan=option_a
11 goto washingtondc
12 )
13 if /i "%toystory%"=="n" goto cleveland
14 goto baltimore
15 )


The script is asking you to make a selection. After making
your selection it will ask you to confirm it. If you
select either Y or N for the confirmation it will take you
back to line 3 as if you had selected an incorrect option.
The second time you answer the confirmation question the
script works successfully. Can anybody explain this?

Thanks,

Chad

Before CMD executes a statement, it first resolves the variables
in the statement and then executes it. Anything within parentheses,
even though it may be multiple lines, is treated as though it were
a single statement. So in your batch file above, beginning with the
left parenthesis at the end of line 4 through the right parenthesis
in line 15, CMD resolves all the variables, including both occurrences
of '%toystory%' (which have no value) *BEFORE* executing line 5, line 6,
etc. From Win2000 on, you have the option to 'delay' the expansion of
variables until actual execution by using '!' instead of '%'. So if
you change your batch file to look like the following, it will probably
work as you intend:

01. @echo off&setlocal ENABLEDELAYEDEXPANSION
02. set /p findingnemo=(A / B)
03.
04. :baltimore
05. if /i "%findingnemo%"=="a" (
06. cls
07. echo You selected option A.
08. echo.
09. set /p toystory=Is this correct? (Y /N^)
10. if /i "!toystory!"=="y" (
11. set peterpan=option_a
12. goto washingtondc
13. )
14. if /i "!toystory!"=="n" goto cleveland
15. goto baltimore
16. )
 
The interpreter is parsing the entire code following the first comparison.

Remove the CLS statement from your script and you can see how the
interpreter parses the command set on a match.

To do what you want to do, you need to break out the confirmation code into
its own function. The following code will give you the result you want.

@echo off
setlocal
set /p findingnemo=(A / B)

:baltimore
if /i "%findingnemo%"=="a" (
cls
echo You selected option A.
echo.
)

call :confirm

goto :eof

:confirm
set /p toystory=Is this correct? (Y /N^)

if /i "%toystory%"=="y" (
set peterpan=option_a
goto washingtondc
)
if /i "%toystory%"=="n" goto cleveland
goto baltimore
)
goto :eof

endlocal

Hope this helps.

Cordially yours,
Jerry G. Young II
 
enabledelayedexpansion!!! Perfect!

i guess I should read set /? before posting questions!

Thanks Phil

Chad
 
enabledelayedexpansion!!! Perfect!
i guess I should read set /? before posting questions!

You could use ".ForExpand", which allows "delayed expansion"
in a CONSISTENT manner across NT/2K/XP/K3.
See (http://TheSystemGuard.com/MtCmds/CommandShorthand/ForExpand.htm)

Also, take a look at the "SET Page" for complete NT/2K/XP/K3 syntax
listings.
(http://TheSystemGuard.com/TheGuardBook/CCS-Int/SET.htm)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!
 
Back
Top