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
Question about IF
Moderator: DosItHelp
Re: Question about IF
I would do it this way:
Regards
aGerman
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
Re: Question about IF
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
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
Re: Question about IF
You have to change the regular expression. Have a look at
findstr /?
Regards
aGerman
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
Re: Question about IF
Hi aGerman
It works. Many thanks.
It works. Many thanks.