Computing.Net > Forums > Programming > Batch file: Nested IF not working!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file: Nested IF not working!

Reply to Message Icon

Name: knhcmp
Date: March 12, 2008 at 15:10:33 Pacific
OS: XP Pro
CPU/Ram: 2 GB
Product: Dell
Comment:

The following code works as expected-
it loops for user's input and detects <ret>, Y/y or N/n:

---------------
@echo off
setLocal EnableDelayedExpansion

:inputloop
set cho=
set /p "cho=>"
if !cho!'==' goto return
if /i %cho%==y goto yes
if /i %cho%==n goto no
goto inputloop

:return
echo return
pause
goto :eof

:yes
echo yes
pause
goto :eof

:no
echo no
pause
goto :eof
---------------

But the code no longer behaves after I add a nested IF structure. Y/y and N/n still work as before, but <ret> now seems to skip the 'goto return' and heads straight for :eof.

How could this be? Help!

---------------
@echo off
setLocal EnableDelayedExpansion

:inputloop
set cho=
set /p "cho=>"
if !cho!'==' (
goto return
) else (
if /i %cho%==y (
goto yes
) else (
if /i %cho%==n (
goto no
) else (
goto inputloop
)
)
)

:return
echo return
pause
goto :eof

:yes
echo yes
pause
goto :eof

:no
echo no
pause
goto :eof
---------------



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: March 13, 2008 at 00:03:27 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

:inputloop
set cho=
set /p "cho=>"
if !cho!'==' (
goto return
) else (
if /i !cho!==y (
goto yes
) else (
if /i !cho!==n (
goto no
) else (
goto inputloop
)
)
)

:return
echo return
pause
goto :eof

:yes
echo yes
pause
goto :eof

:no
echo no
pause


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: knhcmp
Date: March 13, 2008 at 08:57:52 Pacific
Reply:

It works! Thanks, Mechanix2Go.

Looks like I can't mix !cho! and %cho%. But why is that? By the look of it, I'm guessing that !cho! is "inline substitution" (?), while %cho% is simply a variable? But then again, why can't they mix?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file: Nested IF not working!

Batch file nested FOR loops help www.computing.net/answers/programming/batch-file-nested-for-loops-help/15196.html

Help IF/IF NOT EXIST using batch www.computing.net/answers/programming/help-ifif-not-exist-using-batch/15249.html

A batch file HAS STOPPED working. www.computing.net/answers/programming/a-batch-file-has-stopped-working/17436.html