Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: The menu to run the batch  (Read 59397 times)

0 Members and 1 Guest are viewing this topic.

sun_os

    Topic Starter


    Beginner

    The menu to run the batch
    « on: September 16, 2012, 08:51:13 PM »
    I run the batch file used for OS backup and Recover the data from the Storage on the Window.  The source code only for adjust host name and folder name.  The new host backup, it only adjust the folder name and host name. But it sometimes to human error to lead the script run fail. 

    Therefore I want to the script have the menu to select do the backup and recover.  The operator only select the menu option, input the hostname to run the backup or recover.

    May I know any one guy have such experience to use menu option to run the batch jobs ?  Please  advice :'(

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: The menu to run the batch
    « Reply #1 on: September 17, 2012, 07:38:14 AM »
    Back in the day I use to have a menu in my autoexec.bat for options. The options were pretty much Enter ( 1 ) for this option, and Enter ( 2 ) for this other option, and so on. The options were then individual batch files named by number such as 1.bat and 2.bat located in the root of the drive with autoexec.bat and so if you pressed 2 and then the enter key it would process the batched instructions contained within 2.bat

    so if the following was in 2.bat

    Quote
    cls
    @echo. Hello World
    And you enter 2 at the command line

    you would have something like

    Quote
    Hello World
    c:\>

    You can also do away with the @echo. and just use echo. if you use Echo Off at the beginning. This will hide the command prompt until Echo On is executed.

    Quote
    echo off
    cls
    echo. hello world
    pause
    echo on

    This is a description of what the above does:
    1.) Echo Off to hide command prompt
    2.) cls to clean screen
    3.) echo. hello world so "hello world" is displayed ( Print to Screen = echo. ) in this case.
    4.) pause to pause the batches execution until any key is pressed
    5.) Echo On to bring back the command prompt

    So a simple menu can be set up like this named autoexec.bat
    Quote
    Echo Off
    cls
    echo. Please chose from the following options
    echo. -----------------------------------------------------
    echo. Enter ( 1 ) to Start Process 1
    echo. Enter ( 2 ) to Start Process 2
    echo. Enter ( 3 ) to Start Process 3
    echo. Enter ( Q ) to exit menu

    1.bat will contain the batched instructions for option 1
    2.bat will contain the batched instructions for option 2
    3.bat will contain the batched instructions for option 3
    q.bat will contain the batches instructions for (q or Q key - not case sensitive on MS OS ) and at the end of the batch instructions contained within q.bat you might want to issue a Echo On command to bring the command prompt back to the user.

    If you want to have it all contained within a single batch you can use goto statements, but I am trying to make this as simple for you to comprehend as possible. Isolating the batched instructions also makes management of it easier as for instead of having 1 batch with everthing contained within it, you can modify the batch for option 2 etc and if you have a typo your batch will function until you get to option 2 and then you know that the issue is contained within 2.bat etc. If you want to have a single batch such as all choices listed and goto's it can be done in a menu type batch with gotos.

    The above was written for MS OS .... are you doing this on a SUN OS since your user name is SUN_OS? If so  the q and Q will be case sensitive and cls for clear screen I think is "CLEAR" instead of CLS etc. There also may be differences in how the echo works under SUN. Its been a long time since I played with SUN OS

    Salmon Trout

    • Guest
    Re: The menu to run the batch
    « Reply #2 on: September 17, 2012, 09:05:11 AM »
    To turn off echoing the first line of a batch file should be really be

    @echo off

    (i.e. the first character of the line is @). If you just have

    Echo Off

    then those two words will be echoed to the screen, although subsequent commands will not be echoed.

    sun_os

      Topic Starter


      Beginner

      Re: The menu to run the batch
      « Reply #3 on: April 21, 2021, 07:41:57 AM »
      Thank you for all helpful hints,

      I draft the batch file like this, source code as below
      ECHO off
      CLS
      :MENU
      ECHO.
      ECHO ....................................... ........
      ECHO   Select server for Applies Standalone Lan Server
      ECHO ....................................... ........
      ECHO.
      ECHO 1 - Shutdown
      ECHO 2 - Restart
      ECHO 3 - Ping test
      ECHO 4 - Quit
      ECHO.
      SET /p M=Type 1  2  3  4  then press ENTER:
      IF %M%==1 GOTO SHUTDOWN
      IF %M%==2 GOTO RESTART 
      IF %M%==3 GOTO PING 
      IF %M%==4 GOTO QUIT

      :SHUTDOWN
      set /p servername=" Enter the server name "
      set /p yesno=Shutdown the %servername% ? [y/n]
      IF "%yesno%" == "y"  GOTO CONFIRM1
      IF "%yesno%" == "n"  GOTO MENU

      :CONFIRM1
      shutdown -m \\"%servername%" -s

      :RESTART
      set /p servername=" Enter the server name "
      set /p yesno=Reboot the "%servername%" ? [y/n]
      IF "%yesno%" == "y"  GOTO CONFIRM2
      IF "%yesno%" == "n"  GOTO MENU

      :CONFIRM2
      shutdown -m  \\"%servername%" -r

      :PING
      set /p servername=" Enter the server name "
      set /p yesno=Are you ping the "%servername%" ? [y/n]
      IF "%yesno%" == "y"  GOTO CONFIRM3
      IF "%yesno%" == "n"  GOTO MENU

      :CONFIRM3
      ping "%servername%"

      :break
      GOTO QUIT

      :END

      From the source code, I want to enhance the menu to do several things
      1. I want to the operator can return the menu page after ping test.
      2. I want to the operator can runas admin right  account to remote reboot and shutdown
      3. Due to the operator has the right restriction, they cannot do run cmd to run the command. May I know how to process the batch file without cmd ?

      Thanks