批处理文件中的菜单 [英] Menus in Batch File

查看:20
本文介绍了批处理文件中的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不创建批处理文件,因为我只是在运行框或命令提示符中输入我需要的内容,但我正在尝试制作一个只是为了让我访问 Windows 中的基本实用程序并检查内容(我真的不需要它,但我想我爸爸会觉得它有帮助).我对 python 很熟悉(但不熟悉),所以如果将 python 用于这些事情是一个更好的选择,我可以这样做,但是我认为批处理是做这样简单的事情的最佳方式.问题出在我的菜单上.我认为因为我的菜单,它在执行选定的命令之前循环执行所有命令.对此的任何帮助将不胜感激,批处理脚本位于下面的代码框中.

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

推荐答案

如果您根本不测试 errorlevel 值,您可以使用更简单的方法,而只是使用它goto 命令与多个目的地组合在一起.为了使此方法起作用,必须更改标签以使其包含错误级别值.

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

这篇关于批处理文件中的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆