Batch Files - Create a Menu to Execute Commands

Page 1 of 2 12 LastLast
    Batch Files - Create a Menu to Execute Commands

    Batch Files - Create a Menu to Execute Commands

    How to Use a Batch File to Create a Command Prompt Menu to Execute Commands
    Published by
    Designer Media Ltd


    How to Use a Batch File to Create a Command Prompt Menu to Execute Commands

       Information
    This tutorial shows the process one goes through when creating a batch file that opens a command prompt menu allowing you to launch commands, programs and more.

    I will only be executing programs in this tutorial, but you can use this to execute any DOS command that is available in command prompt.


       Tip
    Lines typed in the Courier New font should be added to the document.


    Opening Notepad

    First Open Notepad and Create a New Text Document

    Clearing the Command Window

    The next thing we'll do is tell the batch file not to show the second command we input. This uses the ECHO OFF command. As mentioned, ECHO OFF only hides the next command, if you wish to hide all commands you must use @ECHO OFF.

    ECHO OFF

    Once we've turned off display of the next line, we'll issue another command. This command is CLS. CLS clears the current command window to only show a prompt.

    CLS

    Creating the First Anchor

    An anchor is simply a marker which shows the command prompt what to execute when directed to that specific point in the batch file. The first anchor we'll use will be called MENU because when executed it will show the menu of choices. To make MENU an anchor we'll place a : before it.

    :MENU

    Creating the Menu

    Now we'll actually create the menu. This will be the text that the user sees when he/she opens the batch file and it will also be reverted to after executing a command other than exit. The menu will be comprised of ECHO commands. Not only can an ECHO command turn off display of commands, but it can also display whatever you type after it. If you place a space after the ECHO command, it will print whatever you type onto the command window. If you place a . (period) after the ECHO command it will skip a line.

    ECHO.
    ECHO ...............................................
    ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open Notepad
    ECHO 2 - Open Calculator
    ECHO 3 - Open Notepad AND Calculator
    ECHO 4 - EXIT
    ECHO.

    Creating the Input Area

    Now that we've created the visual menu, we also need to create the place where the user can input his or her choice and execute it. To do this we will use the SET command. This command simply lets the user change one variable or string to another. In other words it will change an input of "1" into an input of GOTO NOTE which will direct the batch file to execute whatever is in the NOTE section. In our SET command we'll give the user 4 choices, one directs the batch file to the NOTE section, another to the CALC section, another to the BOTH section and the last directs the batch file to exit the Window.

    SET /P M=Type 1, 2, 3, or 4 then press ENTER:
    IF %M%==1 GOTO NOTE
    IF %M%==2 GOTO CALC
    IF %M%==3 GOTO BOTH
    IF %M%==4 GOTO EOF

    Creating the Command Sections

    Now we'll create the sections of the batch file that are executed by 1, 2, 3 or 4. These sections will be marked by an anchor so that the batch file knows where to start. The sections that execute programs or commands will also be directed to the MENU at the end. If you wished to exit the command prompt instead of falling back on the menu, you could simply place an EXIT command at the end of the section. We will also be using the CD command. This command changes the directory of the command prompt. While not strictly neccesary in this batch file, it will be neccesary if you execute programs outside of the system32 directory. The START command will be used as well. It simply starts the executable. The GOTO command is the last in each section. It tells the batch file to go to a specific anchor. All of the GOTO commands are anchored to the MENU.

    :NOTE
    cd %windir%\system32\notepad.exe
    start notepad.exe
    GOTO MENU
    :CALC
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU
    :BOTH
    cd %windir%\system32\notepad.exe
    start notepad.exe
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU

    Saving & Creating the Shortcut

    Now that we've finished the batch file, we need to save it. You should save the file as ALL FILES and with a .bat extension. It can be named anything you want. Once you've saved it, you should create a shortcut. This shortcut will allow you to execute the batch file and allow you to change the icon. To change the shortcut's icon simply go to Properties and Change Icon.

    Review

    In this tutorial we created a batch file which shows a command prompt menu through which you can choose to execute programs. This is only one of many uses for batch files. Files can be copied, DOS commands executed, and more. If you wish to use a batch file to create a menu which will allow you to do more, read through tutorials on DOS commands and using batch files. Below is the entire Document.

    ECHO OFF
    CLS
    :MENU
    ECHO.
    ECHO ...............................................
    ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open Notepad
    ECHO 2 - Open Calculator
    ECHO 3 - Open Notepad AND Calculator
    ECHO 4 - EXIT
    ECHO.
    SET /P M=Type 1, 2, 3, or 4 then press ENTER:
    IF %M%==1 GOTO NOTE
    IF %M%==2 GOTO CALC
    IF %M%==3 GOTO BOTH
    IF %M%==4 GOTO EOF
    :NOTE
    cd %windir%\system32\notepad.exe
    start notepad.exe
    GOTO MENU
    :CALC
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU
    :BOTH
    cd %windir%\system32\notepad.exe
    start notepad.exe
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU

       Tip
    For more about Batch files, read this tutorial:
    Microsoft - Batch Files

    For more DOS commands, read this tutorial:
    MS-DOS help and commands









  1. Posts : 1
    Windows 7 Ultimate 32bit
       #1

    Nice Sharing buddy
      My Computer


  2. Posts : 2
    XP 32
       #2

    The crucial shortcoming of this batch routine is the use of the enter key to complete the command. This was easily avoided by using either the CHOICE or DEBUG utility up until and including any 32 bit environment. These utilities are not included anymore in windows 7 and they can not be used in any 64 bit environment.

    Do you have a solution to this dilemma?
      My Computer


  3. Posts : 1,800
    Windows 7 Pro x64 SP1
       #3

    cheebase said:
    The crucial shortcoming of this batch routine is the use of the enter key to complete the command. This was easily avoided by using either the CHOICE or DEBUG utility up until and including any 32 bit environment. These utilities are not included anymore in windows 7 and they can not be used in any 64 bit environment.

    Do you have a solution to this dilemma?
    but it does work in the x64 environment and pressing enter is most likely easier for most people to understand and accomplish. The batch file is great..

    Rich
      My Computer


  4. Posts : 2
    XP 32
       #4

    richnrockville: I referred to the inability to run GETKEY or CHOICE in a 64 bit environment. The batch works fine there.
    No, I disagree. A single key answer in any menu is the preferred way, forget the enter key... So the question remains: is there a way to accomplish that feat.
      My Computer


  5. Posts : 1,800
    Windows 7 Pro x64 SP1
       #5

    cheebase said:
    richnrockville: I referred to the inability to run GETKEY or CHOICE in a 64 bit environment. The batch works fine there.
    No, I disagree. A single key answer in any menu is the preferred way, forget the enter key... So the question remains: is there a way to accomplish that feat.
    Cheebase, most likely not without a third party program which would have to be installed that the batch file would run. I think that for brevities sake and the ease by which many people could modify the batch file and make it run, this simple batch file method is most likely the cleanest and easiest to understand.

    I could write the program in compliled basic with an onkey but I think that this batch file will work for the majority of users.

    After all anyone with windows and notepad can make changes and get it done.

    Rich
      My Computer


  6. Posts : 1
    Windows 7 Ultimate x64
       #6

    I found a turnaround to make the "single key feat". Instead of just using SET or CHOICE, I used both together and it works on x64:

    ECHO OFF
    CLS
    :MENU
    ECHO.
    ECHO ...............................................
    ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
    ECHO ...............................................
    ECHO.
    ECHO 1 - Open Notepad
    ECHO 2 - Open Calculator
    ECHO 3 - Open Notepad AND Calculator
    ECHO 4 - EXIT
    ECHO.
    CHOICE /C:1234
    IF ERRORLEVEL 1 SET M=1
    IF ERRORLEVEL 2 SET M=2
    IF ERRORLEVEL 3 SET M=3
    IF ERRORLEVEL 4 SET M=4

    IF %M%==1 GOTO NOTE
    IF %M%==2 GOTO CALC
    IF %M%==3 GOTO BOTH
    IF %M%==4 GOTO EOF
    :NOTE
    cd %windir%\system32\notepad.exe
    start notepad.exe
    GOTO MENU
    :CALC
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU
    :BOTH
    cd %windir%\system32\notepad.exe
    start notepad.exe
    cd %windir%\system32\calc.exe
    start calc.exe
    GOTO MENU
    Last edited by Cuholvke; 20 Dec 2011 at 13:07.
      My Computer


  7. Posts : 1
    64 bit Windows 7 home premium
       #7

    When I try to make a menu open another batch program, it works fine, However, when I try to make an opened batch program open another program, like notepad, it says
    The system cannot find the batch label specified -"Label name here".
    Any help on the matter?
      My Computer


  8. Posts : 31
    win7
       #8

    Build a dynamic menu like this:

    Code:
     
    @echo off
    
    REM 
    REM Tweakradje 2014, V2
    REM 
    REM Find all Packages and remove the ones indicated
    REM
    REM Good info: http://technet.microsoft.com/en-us/library/hh825265.aspx
    REM
    REM  DISM /online /Get-Packages /Format=Table
    REM  DISM /online /Get-Drivers /Format=Table
    REM  DISM /online /Get-Features /Format=Table to enable/disable/remove
    REM  
    
    Title Remove Windows Packages from local store
    Color 17
    Setlocal ENABLEDELAYEDEXPANSION
    
    :BUILDMENU
    Echo.
    Echo Getting package list...
    Echo.
    SET /A MAXITEM=0
    REM ### Get the output lines from the command and store them in Environment variables
    REM ### We want the output of Findstr.exe, Not DISM.exe, hence the ^ symbol
    For /f "skip=3 delims=" %%M in ('"DISM /online /Get-Packages /Format=Table"^|FindStr :') do (
    	Set /A MAXITEM=!MAXITEM!+1
    	Set MENUITEM!MAXITEM!=%%M
    )
    
    :SHOWMENU
    Cls
    Echo.
    Echo.
    Set CHOICE=0
    REM ### Build the menu on screen and count the items
    For /L %%I in (1,1,!MAXITEM!) do Echo    %%I. !MENUITEM%%I!
    Echo.
    SET /P CHOICE="Select (superseeded?) Package to remove or Q to quit: "
    Echo.
    IF %CHOICE%==q Goto BYE
    IF %CHOICE%==Q Goto BYE
    IF %CHOICE% Gtr !MAXITEM! Goto SHOWMENU
    IF %CHOICE%==0 Goto SHOWMENU
    
    REM ### Get the text for selected item from the proper Environment variable
    Echo.
    REM ### We want only left part of the first | with package name
    For /f "delims=| " %%S in ("!MENUITEM%CHOICE%!") Do Set PKGNAME=%%S
    Echo Removing "%PKGNAME%"
    Echo.
    Set /P YESNO="Are you sure (Y/N): "
    If Not %YESNO%==y If Not %YESNO%==Y Goto SHOWMENU
    DISM /online /Remove-Package /PackageName:"%PKGNAME%"
    pause
    Goto BUILDMENU
    
    
    :BYE
    Color
    EndLocal
    Last edited by tweakradje; 26 Aug 2014 at 16:09. Reason: code box, V2
      My Computer


  9. Posts : 31
    win7
       #9

    :selectfile
    SETLOCAL ENABLEDELAYEDEXPANSION << DELAYED EXPANSION on for counter in For loop
    SET /A MAXITEM=0
    Echo.
    For %%F in (folder1/*.TXT) do (
    SET /A MAXITEM+=1
    SET MENUITEM!MAXITEM!=%%F << Fill n local env vars, one for each file, called MENUITEM1...n
    Echo !MAXITEM!. %%F
    )
    Echo.
    If !MAXITEM!==0 Echo No TXT in "folder1" folder & call :delay & Goto start << No apk files then go back
    SET /P CHOICE=Select TXT to work on:
    SET MENUITEM=!MENUITEM%CHOICE%! << Get the stored filename from MENUITEMx
    SETLOCAL DISABLEDELAYEDEXPANSION << Very important for next ENDLOCAL that doesn't like the delayedexpansion var
    ENDLOCAL & SET selectedtxtfile=%MENUITEM% << put the local MENUITEM var into the global var selectedtxtfile
    Goto start

    :delay
    SETLOCAL
    REM %1 like call :delay 5
    SET N=%1
    If Not Defined N Set N=2
    Ping -n %N% -w 1000 127.255.255.255 > nul
    ENDLOCAL
    Exit /b
    Last edited by tweakradje; 26 Aug 2014 at 16:11. Reason: Hi Brink, I hold this one short, please consider codebox since it loses colors and makes it harder to understand. Thanks
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:05.
Find Us