Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menus in Batch File

I don't usually create batch files as I just type what I need into the run box or the command prompt but I'm trying to make one just to let me access basic utilities in windows and check up on things (I really don't need it but I think my dad would find it helpful). I am familiar (but new) with python so if using python for these things is a better option I can do that, however I thought batch was the best way of doing something as simple as this. The problem is with my menu. I think because of my menu , it is cycling through all of the commands before doing the command selected. Any help with this will be fully appreciated, the batch script is in a code box below.

echo off
:menu
echo This is a simple cleanup and repair utility. Please select an option:
echo 1 - Check the hard disk c:\ for errors and inconsistancies. 
echo 2 - Renew the IP address 
echo 3 - View IP Address information
echo 4 - Check internet connection  by pinging http://www.google.co.uk/
echo 5 - Start disk cleanup utility
echo 6 - ping 192.168.0.1
echo 7 - ping 192.168.1.1
echo 8 - Open notepad
choice /n /c:12345678 /M "Choose an option (1-8) "
IF ERRORLEVEL == 1 GOTO CHKDSK
IF ERRORLEVEL == 2 GOTO RENEW
IF ERRORLEVEL == 3 GOTO DISPLAYIP
IF ERRORLEVEL == 4 GOTO PINGGOOGLE
IF ERRORLEVEL == 5 GOTO CLEANMGR
IF ERRORLEVEL == 6 GOTO PING0
IF ERRORLEVEL == 7 GOTO PING1
IF ERRORLEVEL == 8 GOTO STARTNOTE
:CHKDSK
CHKDSK C:
PAUSE
goto menu
:RENEW
IPCONFIG /RENEW
PAUSE
goto menu
:DISPLAYIP
IPCONFIG /ALL
PAUSE
goto menu
:PINGGOOGLE
PING HTTP://WWW.GOOGLE.CO.UK/
PAUSE
goto menu
:CLEANMGR
CLEANMGR
PAUSE
goto menu
:PING0
PING 192.168.0.1
PAUSE
goto menu
:PING1
PING 192.168.1.1
PAUSE
goto menu
:STARTNOTE
START NOTEPAD
PAUSE
goto menu
like image 1000
Matthew Sansom Avatar asked Oct 03 '22 19:10

Matthew Sansom


2 Answers

You may use a much simpler approach if you don't test the errorlevel value at all, but just use it to assemble a goto command with multiple destinations. In order for this method to work, the labels must be changed so they include the errorlevel values.

echo off
:menu
echo This is a simple cleanup and repair utility. Please select an option:
echo 1 - Check the hard disk c:\ for errors and inconsistancies. 
echo 2 - Renew the IP address 
echo 3 - View IP Address information
echo 4 - Check internet connection  by pinging http://www.google.co.uk/
echo 5 - Start disk cleanup utility
echo 6 - ping 192.168.0.1
echo 7 - ping 192.168.1.1
echo 8 - Open notepad
choice /n /c:12345678 /M "Choose an option (1-8) "
GOTO LABEL-%ERRORLEVEL%

:LABEL-1 CHKDSK
CHKDSK C:
PAUSE
goto menu
:LABEL-2 RENEW
IPCONFIG /RENEW
PAUSE
goto menu
:LABEL-3 DISPLAYIP
IPCONFIG /ALL
PAUSE
goto menu
:LABEL-4 PINGGOOGLE
PING HTTP://WWW.GOOGLE.CO.UK/
PAUSE
goto menu
:LABEL-5 CLEANMGR
CLEANMGR
PAUSE
goto menu
:LABEL-6 PING0
PING 192.168.0.1
PAUSE
goto menu
:LABEL-7 PING1
PING 192.168.1.1
PAUSE
goto menu
:LABEL-8 STARTNOTE
START NOTEPAD
PAUSE
goto menu
like image 162
Aacini Avatar answered Oct 07 '22 17:10

Aacini


Change the ErrorLevel part to

IF %ERRORLEVEL%==1 GOTO CHKDSK
IF %ERRORLEVEL%==2 GOTO RENEW
IF %ERRORLEVEL%==3 GOTO DISPLAYIP
IF %ERRORLEVEL%==4 GOTO PINGGOOGLE
IF %ERRORLEVEL%==5 GOTO CLEANMGR
IF %ERRORLEVEL%==6 GOTO PING0
IF %ERRORLEVEL%==7 GOTO PING1
IF %ERRORLEVEL%==8 GOTO STARTNOTE
goto menu

Note that the if command comparisons are space sensitive therefore, "0 "==" 0" will not be equal.

like image 25
David Ruhmann Avatar answered Oct 07 '22 19:10

David Ruhmann