Login Register






Menus in one batch file filter_list
Author
Message
Menus in one batch file #1
My example of menus in batch files, using two different menus in one file. This is geared towards people who are just starting to learn batch.

REM is a comment line, it has no effect on the operation of the code. @Echo off is a code that is essential to scripts where you don't want the user to see the commands themselves. Echo prints stuff on the screen that you want it to. Lines starting with : represent bookmarks, and you can essentially make them anything you want, but don't use spaces unless you want to "" after goto. Goto refers to and jumps to bookmarks, signaled by : lines. Set /P actually controls the menu, A or B as in "set /p A=..." Sets the user's response to the A variable, and after the = it prints that to the user. If %A%==# states that if the user response is that number, it will do the command after it. To make it easier I used goto.

Sorry if it was too simple, was just trying to make a contribution to the forum.

Code:
REM Sample batch menu. @Echo off :mainmenu cls echo This is the main menu. echo Use the keyboard to select an option below. echo 1. Menu 2 echo 2. Exit Batch set /p A=Press 1 or 2 and then press enter. IF %A%==1 goto menu2 IF %A%==2 goto end :menu2 cls echo This is the second menu echo 1. Notepad echo 2. Back to Main Menu echo 3. Exit Batch set /p B=Press 1, 2, or 3 and press enter. IF %B%==1 goto notepad IF %B%==2 goto mainmenu IF %B%==3 goto end :notepad cls notepad goto menu2 :end

Reply

RE: Menus in one batch file #2
I've actually toyed around a lot with this setup. If you want, you could make an infinite number of options and your 'set' isn't limited to a single letter. also, if you want a clean line for input you can do this:
Code:
echo input your selection set /p whatever= if %whatever%==choiceA blahblahblah if %whatever%==choiceB blahblahnlah
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: Menus in one batch file #3
(03-08-2014, 06:51 PM)w0lf Wrote: I've actually toyed around a lot with this setup. If you want, you could make an infinite number of options and your 'set' isn't limited to a single letter. also, if you want a clean line for input you can do this:
Code:
echo input your selection set /p whatever= if %whatever%==choiceA blahblahblah if %whatever%==choiceB blahblahnlah
I know that, I was just using single letters to make it easier for beginners in batch menus.

Reply

RE: Menus in one batch file #4
Code:
echo 2. Exit Batch

The semantics of such an option would be the same as saying 'Exit C++' in a C++ program. Batch is a language, cmd is the interface.

Code:
IF %A%==2 goto end

You can also just call exit. Then you could avoid making another marker labelled 'end'. But the way your first menu is written:
Code:
IF %A%==1 goto menu2 IF %A%==2 goto end :menu2

You could've just wrote:
Code:
IF %A%==2 goto end

Or 'exit' instead of 'goto end' as I've suggested, because currently there's nothing stopping the program from falling through to :menu2 as long as the input is not 2. It could even be 9999.

For a good tutorial, you should've explained handling options specified that were invalid IMO, not bad though...

I like using choice instead for menus.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Menus in one batch file #5
(03-09-2014, 11:45 AM)ArkPhaze Wrote:
Code:
echo 2. Exit Batch

The semantics of such an option would be the same as saying 'Exit C++' in a C++ program. Batch is a language, cmd is the interface.

Code:
IF %A%==2 goto end

You can also just call exit. Then you could avoid making another marker labelled 'end'. But the way your first menu is written:
Code:
IF %A%==1 goto menu2 IF %A%==2 goto end :menu2

You could've just wrote:
Code:
IF %A%==2 goto end

Or 'exit' instead of 'goto end' as I've suggested, because currently there's nothing stopping the program from falling through to :menu2 as long as the input is not 2. It could even be 9999.

For a good tutorial, you should've explained handling options specified that were invalid IMO, not bad though...

I like using choice instead for menus.

Valid points, but if I were to write it for a production script, I probably would've thrown an if statement that would've produced an error message or closed if it detected an unspecified input. I was just trying to cut the content back a bit for beginners learning how to batch.

I'm working on one that I'll publish in the near future that will be geared towards people with more experience, based off of scripts I write for my consulting business.

Reply

RE: Menus in one batch file #6
In my opinion, function are better and it gives you a cleaner code ...

Reply

RE: Menus in one batch file #7
(03-09-2014, 10:51 PM)hunt3r972 Wrote: In my opinion, function are better and it gives you a cleaner code ...

Thanks for the mention. I'll have to give it a try. I have to rewrite my software installation scripts for my catalog of Windows images, with the newer versions of the software the developers like Mozilla, etc. have changed their main download urls. Ninite only allows 3 uses per IP per day and my ISP throttles VPN and SSH traffic.

Reply

RE: Menus in one batch file #8
This is how I do:

Code:
function menu(){ echo echo "MENU" echo "1. menu 1" echo "2. menu 2" echo "3. menu 3" echo "4. Exit" } function menu1(){ your code } function menu2(){ your code } function menu3(){ your code } function quit(){ exit } menuselect=0; while [ $menuselect -ne 4 ] do menu read -p "Choose a menu: " selectionmenu case $selectionmenu in 1) menu1;; 2) menu2;; 3) menu3;; 4) quit;; esac done

Reply

RE: Menus in one batch file #9
(03-12-2014, 10:31 AM)hunt3r972 Wrote: This is how I do:

Code:
function menu(){ echo echo "MENU" echo "1. menu 1" echo "2. menu 2" echo "3. menu 3" echo "4. Exit" } function menu1(){ your code } function menu2(){ your code } function menu3(){ your code } function quit(){ exit } menuselect=0; while [ $menuselect -ne 4 ] do menu read -p "Choose a menu: " selectionmenu case $selectionmenu in 1) menu1;; 2) menu2;; 3) menu3;; 4) quit;; esac done

Do you know what you're talking about? That's not batch.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Menus in one batch file #10
@ArkPhaze Oh crap Batch looks like Bash I did a mistake sorry

Reply