Question about IF

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

Question about IF

#1 Post by dukdpk » 25 Mar 2011 00:12

Hi All
Please go through following bat file.


@echo off
:menu
1 Generate report staff
2 Generate report subjects
3 Generate report class
4 exit

echo.
set /p sample=Type option:

if "%sample%"=="1" call r1.bat
if "%sample%"=="2" call r2.bat
if "%sample%"=="3" call r3.bat
if "%sample%"=="4" goto Quit
if "%web%" gtr "4" goto Message
if "%web%" lss "1" goto Message


goto menu

:Message
echo you entered incorrect option

pause
goto menu

:Quit
exit



My question is that when I enter option, It perform the action correctly but when I enter nothing, perform the last action. For example when I enter 1 for option then generate the staff report. Then I enter nothing for the option it also perform last action which is generate the staff report. Please could anyone to explain for reson that. Acutually I expect to display "You entered incocorrect option" message when I enter nothing for option.

Thank You

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Question about IF

#2 Post by aGerman » 25 Mar 2011 15:35

I would do it this way:

Code: Select all

@echo off &setlocal
:menu
cls
echo 1 Generate report staff
echo 2 Generate report subjects
echo 3 Generate report class
echo 4 exit

echo(
set "sample="
set /p "sample=Type option: "
setlocal enabledelayedexpansion
echo("!sample!"|findstr /x "\"[1-4]\"" >nul ||(endlocal &echo you entered an incorrect option &pause &goto menu)
endlocal
if "%sample%"=="1" call "r1.bat"
if "%sample%"=="2" call "r2.bat"
if "%sample%"=="3" call "r3.bat"
if "%sample%"=="4" goto :eof
goto menu


Regards
aGerman

dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

Re: Question about IF

#3 Post by dukdpk » 28 Mar 2011 01:07

Hi aGerman

It works. Thank you. Could you explain, when assume 1,2,3 and 4 options are become as a001, a003, a006, a001-a006 how will be the code changed?

i.e.

@echo off &setlocal
:menu
cls
echo a001 Generate report staff
echo a003 Generate report subjects
echo a006 Generate report class
echo a001-a006 exit

if "%sample%"=="a001" call "r1.bat"
if "%sample%"=="a003" call "r2.bat"
if "%sample%"=="a006" call "r3.bat"
if "%sample%"=="a001-a006" goto :eof
goto menu


Thank You

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Question about IF

#4 Post by aGerman » 28 Mar 2011 14:06

You have to change the regular expression. Have a look at
findstr /?

Code: Select all

@echo off &setlocal
:menu
cls
echo a001 Generate report staff
echo a003 Generate report subjects
echo a006 Generate report class
echo a001-a006 exit

echo(
set "sample="
set /p "sample=Type option: "
setlocal enabledelayedexpansion
echo("!sample!"|findstr /x "\"a00[1,3,6]\" \"a001-a006\"" >nul ||(endlocal &echo you entered an incorrect option &pause &goto menu)
endlocal
if "%sample%"=="a001" call "r1.bat"
if "%sample%"=="a003" call "r2.bat"
if "%sample%"=="a006" call "r3.bat"
if "%sample%"=="a001-a006" goto :eof
goto menu


Regards
aGerman

dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

Re: Question about IF

#5 Post by dukdpk » 30 Mar 2011 00:52

Hi aGerman

It works. Many thanks.

Post Reply