Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a batch file with multiple options

Tags:

batch-file

cmd

Hi I want to create a batch which performs more than one operations. Like it should display

1.Restart
2.Shutdown
3.Close all Windows
4.Log off
5.Switch User

Then "Enter your choice:(User Choice) Then it should perform the operation

Can you help in this?

like image 211
S.Siva Avatar asked Feb 16 '12 07:02

S.Siva


People also ask

How do I run multiple commands in one batch file?

Instead of scheduling multiple Windows Tasks that may overlap, use the "start /wait" command a batch file (. bat) to automatically run multiple commands in sequential order.

How do I select multiple options in CMD?

To select multiple items in a list, hold down the Ctrl (PC) or Command (Mac) key. Then click on your desired items to select. All of the items you have selected should be highlighted with a different-colored background. Note: Be sure to hold the Ctrl (PC) or Command (Mac) key down while selecting multiple items.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


3 Answers

This should get you started. The CHOICE command is available in most versions of Windows but may require that you get the Windows NT 4 resource kit. The CHOICE command is available in Windows 7.

@ECHO OFF
CLS
ECHO 1.Restart
ECHO 2.Shutdown
ECHO 3.Close all Windows
ECHO 4.Log off
ECHO 5.Switch User
ECHO.

CHOICE /C 12345 /M "Enter your choice:"

:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 5 GOTO SwitchUser
IF ERRORLEVEL 4 GOTO Logoff
IF ERRORLEVEL 3 GOTO CloseAllWindows
IF ERRORLEVEL 2 GOTO Shutdown
IF ERRORLEVEL 1 GOTO Restart

:Restart
ECHO Restart (put your restart code here)
GOTO End

:Shutdown
ECHO Shutdown (put your shutdown code here)
GOTO End

:CloseAllWindows
ECHO Close All Windows (put your close all windows code here)
GOTO End

:Logoff
ECHO Logoff (put your log off code here)
GOTO End

:SwitchUser
ECHO Switch User (put your switch user code here)
GOTO End

:End
like image 85
Steven Schroeder Avatar answered Sep 30 '22 14:09

Steven Schroeder


Now someone just put is this your homework... I almost don't want to paste this! lol

but... I will anyway :p

echo off
:begin
echo Select a task:
echo =============
echo -
echo 1) Option 1
echo 2) Option 2
echo 3) Option 3
echo 4) Option 4
echo -
set /p op=Type option:
if "%op%"=="1" goto op1
if "%op%"=="2" goto op2
if "%op%"=="3" goto op3
if "%op%"=="4" goto op4

echo Please Pick an option:
goto begin


:op1
echo you picked option 1
goto begin

:op2
echo you picked option 2
goto begin

:op3
echo you picked option 3
goto begin

:op4
echo you picked option 4
goto begin

:exit
@exit

You can swap goto begin to goto exit in each of the sections if you want it to run the command then close the batch file, which is what I would recommend, seeing as you want to shutdown.

Hope this helps.

Martyn

like image 41
SmithMart Avatar answered Sep 30 '22 14:09

SmithMart


This way is even better...

@echo off 
title A better way
color 0a
echo.
echo 1.restart
echo 2.shutdown
echo 3.close all windows
echo 4.log off
echo 5.switch user
echo.

set /p a=
IF %a%==1 (put in restart code)
IF %a%==2 (put in shutdown code)
IF %a%==3 (put in close all Windows code)
IF %a%==4 (put in log out code)
IF %a%==5 (put in switch user code)
like image 44
Anonymous Avatar answered Sep 30 '22 16:09

Anonymous