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
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
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.
The errorlevel is a bit funny. When you compare like that, it actually checks if the number is less than or equal. So to make it work your current way, you need to order the tests in reverse.
The other way to do it is use the environment variable:
if "%errorlevel%"=="4" goto :PINGGOOGLE
It's usually a good idea to put goto :menu
after all the tests, just in case none of the tests pass. For example, if the user presses Ctrl-C, the errorlevel comes out as zero. In your code (after you fix the tests), it will fall through to the :CHKDSK
section.