Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

.bat or .cmd file error checking

  • 01-08-2014 11:47am
    #1
    Registered Users, Registered Users 2 Posts: 3,829 ✭✭✭


    Just wondering if anybody here has any experiencing with writing .bat or .cmd files?

    I have the following .cmd file (My first attempt at windows scripting)
    @ECHO OFF
    CLS
    
    :MENU
    
    echo.
    echo.
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo _/                                                                      _/
    echo _/ Enter the number corresponding to the device you wish to connect to  _/
    echo _/ Enter "x" to exit                                                    _/
    echo _/                                                                      _/
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo.
    echo Lab Devices
    echo.
    echo  1 - Router1
    echo  2 - Router2
    echo  3 - Router3
    echo  4 - Router4
    echo  5 - Router5
    echo  6 - Router6
    echo  7 - Router7
    echo  8 - Router8
    echo  9 - Router9
    echo 10 - Router10
    echo 11 - Router11
    echo.
    echo  x  - To exit 
    echo.
    echo. 
    set /P M=Enter Choice Here 
    echo.
    
    if %M%==1 goto R1
    if %M%==2 goto R2
    if %M%==3 goto R3
    if %M%==4 goto R4
    if %M%==5 goto R5
    if %M%==6 goto R6
    if %M%==7 goto R7
    if %M%==8 goto R8
    if %M%==9 goto R9
    if %M%==10 goto R10
    if %M%==11 goto R11
    if %M%==x goto EOF
    
    :R1
    start putty.exe telnet://xx.xx.xx.xx 1
    goto MENU
    :R2
    start putty.exe telnet://xx.xx.xx.xx 2
    goto MENU
    :R3
    start putty.exe telnet://xx.xx.xx.xx 3
    goto MENU
    :R4
    start putty.exe telnet://xx.xx.xx.xx 4
    goto MENU
    :R5
    start putty.exe telnet://xx.xx.xx.xx 5
    goto Menu
    :R6
    start putty.exe telnet://xx.xx.xx.xx 6
    goto MENU
    :R7
    start putty.exe telnet://xx.xx.xx.xx 7
    goto MENU
    :R8
    start putty.exe telnet://xx.xx.xx.xx 8
    goto MENU
    :R9
    start putty.exe telnet://159.107.175.120 9
    goto MENU
    :R10
    start putty.exe telnet://xx.xx.xx.xx 10
    goto MENU
    :R11
    start putty.exe telnet://xx.xx.xx.xx 11
    goto MENU
    

    This file produces a menu of routers to connect to. The user enters a number depending on which device they wish to connect to, 1 for R1, 2 for R2 etc.

    This all works great as long as the user enters a correct value, 1-11 for a device or "x" to exit.

    What I would like to add is an error checker that if a users enters anything other than 1-11 or x an error message is printed on a blank screen (either the original screen or a second screen) for a period of time and then the menu is presented again.

    I have no experience with windows scripting, I usually work with Unix/Linux systems. So wondering if there are any Windows scripting gurus out there who could give me some pointers?

    Any and all help would be much appreciated

    TIA


Comments

  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,086 Mod ✭✭✭✭Fysh


    Just wondering if anybody here has any experiencing with writing .bat or .cmd files?

    I have the following .cmd file (My first attempt at windows scripting)
    @ECHO OFF
    CLS
    
    :MENU
    
    echo.
    echo.
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo _/                                                                      _/
    echo _/ Enter the number corresponding to the device you wish to connect to  _/
    echo _/ Enter "x" to exit                                                    _/
    echo _/                                                                      _/
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo.
    echo Lab Devices
    echo.
    echo  1 - Router1
    echo  2 - Router2
    echo  3 - Router3
    echo  4 - Router4
    echo  5 - Router5
    echo  6 - Router6
    echo  7 - Router7
    echo  8 - Router8
    echo  9 - Router9
    echo 10 - Router10
    echo 11 - Router11
    echo.
    echo  x  - To exit 
    echo.
    echo. 
    set /P M=Enter Choice Here 
    echo.
    
    if %M%==1 goto R1
    if %M%==2 goto R2
    if %M%==3 goto R3
    if %M%==4 goto R4
    if %M%==5 goto R5
    if %M%==6 goto R6
    if %M%==7 goto R7
    if %M%==8 goto R8
    if %M%==9 goto R9
    if %M%==10 goto R10
    if %M%==11 goto R11
    if %M%==x goto EOF
    
    :R1
    start putty.exe telnet://xx.xx.xx.xx 1
    goto MENU
    :R2
    start putty.exe telnet://xx.xx.xx.xx 2
    goto MENU
    :R3
    start putty.exe telnet://xx.xx.xx.xx 3
    goto MENU
    :R4
    start putty.exe telnet://xx.xx.xx.xx 4
    goto MENU
    :R5
    start putty.exe telnet://xx.xx.xx.xx 5
    goto Menu
    :R6
    start putty.exe telnet://xx.xx.xx.xx 6
    goto MENU
    :R7
    start putty.exe telnet://xx.xx.xx.xx 7
    goto MENU
    :R8
    start putty.exe telnet://xx.xx.xx.xx 8
    goto MENU
    :R9
    start putty.exe telnet://159.107.175.120 9
    goto MENU
    :R10
    start putty.exe telnet://xx.xx.xx.xx 10
    goto MENU
    :R11
    start putty.exe telnet://xx.xx.xx.xx 11
    goto MENU
    

    This file produces a menu of routers to connect to. The user enters a number depending on which device they wish to connect to, 1 for R1, 2 for R2 etc.

    This all works great as long as the user enters a correct value, 1-11 for a device or "x" to exit.

    What I would like to add is an error checker that if a users enters anything other than 1-11 or x an error message is printed on a blank screen (either the original screen or a second screen) for a period of time and then the menu is presented again.

    I have no experience with windows scripting, I usually work with Unix/Linux systems. So wondering if there are any Windows scripting gurus out there who could give me some pointers?

    Any and all help would be much appreciated

    TIA

    For the script you've got, the easiest way to achieve this is to add the following lines after "if %M%==x goto EOF":
    :: If invalid selection is entered, menu loops around
    echo Invalid choice entered, please try again.
    cls
    goto menu
    

    The :: marks out the comment. Since you've already got each choice jumping to further down the command sequence, you can just use a default option at the end.

    One thing you may want to consider is using the /wait parameter on the start command (more info) if you only expect one session to be active at a time.


  • Registered Users, Registered Users 2 Posts: 3,829 ✭✭✭TommyKnocker


    Fysh wrote: »
    For the script you've got, the easiest way to achieve this is to add the following lines after "if %M%==x goto EOF":
    :: If invalid selection is entered, menu loops around
    echo Invalid choice entered, please try again.
    cls
    goto menu
    
    The :: marks out the comment. Since you've already got each choice jumping to further down the command sequence, you can just use a default option at the end.

    One thing you may want to consider is using the /wait parameter on the start command (url=http://ss64.com/nt/start.html]more info[/url]) if you only expect one session to be active at a time.

    Thanks you, this is almost what I want but the error messages went off the screen too quickly. Changed it to
    :: If invalid selection is entered, menu loops around
    cls
    echo.
    echo. 
    echo.
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo _/                                                _/
    echo _/          Invalid choice entered.               _/
    echo _/          Please enter 1-11 or "x" to exit.     _/
    echo _/                                                _/
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo.
    echo.
    TIMEOUT /T 10
    ::cls
    goto menu
    

    and it works perfect.

    Thanks for the tip, it is much appreciated :)


  • Moderators, Arts Moderators, Regional Abroad Moderators Posts: 11,086 Mod ✭✭✭✭Fysh


    Thanks you, this is almost what I want but the error messages went off the screen too quickly. Changed it to
    :: If invalid selection is entered, menu loops around
    cls
    echo.
    echo. 
    echo.
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo _/                                                _/
    echo _/          Invalid choice entered.               _/
    echo _/          Please enter 1-11 or "x" to exit.     _/
    echo _/                                                _/
    echo _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    echo.
    echo.
    TIMEOUT /T 10
    ::cls
    goto menu
    

    and it works perfect.

    Thanks for the tip, it is much appreciated :)

    The other option for displaying error messages is to put pause in there - the message stays on screen until a key is pressed, then the script proceeds as normal.

    Out of curiousity, what versions of Windows are you dealing with? If you're dealing mainly with 7 or 8, it's worth looking into PowerShell rather than batch files in future - PS is much closer to bash, and has a lot of built-in functions (especially as of version 4) that make some tasks much easier than they would be in batch files.


Advertisement