【问题标题】:How to write a batch file with offering some options for user?如何编写批处理文件并为用户提供一些选项?
【发布时间】:2015-08-13 14:03:36
【问题描述】:

我想我有我想要的(如下所述),再次感谢您,但请再提供两个选项...当我想在子菜单中添加两个下一个选项时,例如“B”,例如继续开始和“E”像退出。它会是这样的:

你好,我是Zedd,请在下面选择一个应用程序:

  1. 记事本
  2. 计算
  3. 其他

当我像其他人一样触摸“3”时,我会得到下一个菜单:

  1. FFox
  2. IE
  3. 或 B. 返回
  4. 或E.退出

我该怎么做?我可以只使用数字 0-9 还是也可以使用 10,11 等?

代码示例可能是这样的?

@echo off  
:begin  
echo.  
echo Hello, I'm Zedd...  
echo.  
echo Choose:
echo.  
echo 1. Notepad  
echo 2. Calc  
echo 3. Other  
echo.  
choice /c 123  
if %errorlevel%==1 goto :Notepad  
if %errorlevel%==2 goto :Calc 
if %errorlevel%==3 goto :Other

:Notepad  
start notepad.exe  
goto :eof 

:Calc  
start calc.exe  
goto :eof 

:Other  
echo.  
echo Choose:  
echo.  
echo 1. FFox  
echo 2. IE  
echo 3 or B. Back
echo 4 or E. Exit  
echo.  
choice /c 1234(maybe 12be if I can use also b and e, or I can use 3,4... is possible here use 10,11,12 etc.?)    
if %errorlevel%==1 "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"  
if %errorlevel%==2 "C:\Program Files\Internet Explorer\iexplore.exe"  
if %errorlevel%==3 goto :begin?  
if %errorlevel%==4 exit?  
goto :other (I want to stay in other for choosing next apps but I want to be able to go back or exit)

另外,我可以用note来做吗,所以当我选择FFox时会有消息“你选择了FFox”等?

希望我描述清楚,如果你能帮助我,我会很高兴,谢谢!


您能否告诉我如何使用包含不同选项和操作的批处理文件编写CMD 中的命令

我已经研究了我可以使用的功能的一个很好的描述,但我无法将它们在功能上组合在一起。

这是我在 Windows 上运行批处理文件时想要的输出消息示例:

我需要做批处理,例如“test.bat”

我认为这是最简单的方法。在这批中,我想要一些文本和一些选择。例如下面:

Hello, I am Zedd, here is few options for you, please choose one. 

1. Run notepad.exe 
2. Other apps

输入数字 1 后,记事本打开。输入数字 2 后,应该会显示更多选项:

1. Firefox
2. More informations
   etc.

我需要在 Windows 上运行这个批处理文件。

最好的方法是什么?

它可以是全屏或仅在一个窗口内。

我还需要打开的应用程序或图片等。

【问题讨论】:

  • 它是 实际 MS-DOS 还是只是 cmd 窗口?您是否真的在运行 MS-DOS 6(或您正在使用的旧系统上的任何版本)?因为这两者之间实际上存在巨大的差异,并且某些命令对另一个不起作用。
  • 问题指出“我的批处理文件在 Windows 上运行”。
  • 只是cmd windows,这里有哪些可能性?是的,你是对的,很抱歉,如果可能的话,我会纠正它......
  • 全屏?不带批次。打开程序或应用程序?是的(例如只是notepad.exe)。使用选项?是 - 如果程序或应用程序支持它们。如果您不说明具体问题,很难为您提供帮助。
  • 好的,所以我需要的都是可能的,但我不知道该怎么做......所以我尝试再次描述它。我需要做批处理,例如“test.bat”

标签: windows batch-file


【解决方案1】:

已编辑以显示第二级选择的用法)

只是echochoice 命令的组合:

@echo off
:begin
echo Hello, I am Zedd, here is few options for you, please choose one. 
echo 1. Run notepad.exe 
echo 2. Other apps
choice /c 12
if %errorlevel%==1 goto :notepad
if %errorlevel%==2 goto :other    
:notepad
echo you choosed Notepad.
start notepad.exe
goto :eof

:other
echo 1. Mozilla Firefox
echo 2. Microsoft Internet Explorer
echo 3. MyBatchFile
echo B. back to main menue
echo E. exit
choice /c 123B
if %errorlevel%==1 "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
if %errorlevel%==2 "C:\Program Files\Internet Explorer\iexplore.exe"
if %errorlevel%==3 (
  echo you choosed MyBatchFile
  call "c:\test\mybatch.bat"
  goto :begin
)
if %errorlevel%==4 (
  echo you entered "B" to go back to main menue
  goto :begin
)
if %errorlevel%==5 (
  echo bye bye...
  exit /b
)
goto :other

可以在choice中添加/n参数来抑制[1,2]?

编辑 02-sep(请不要更改您的问题,这可能会使已有的答案无效;请提出一个新问题,参考这个旧问题)

“我该怎么做?我可以只使用数字 0-9 还是也可以使用 10,11 等?”

没有。 choice 对按键做出反应,而不是对输入字符串做出反应,因此只有单个数字或字母。虽然3 or B. Back 是可能的:choice /c 123b4e 将返回错误级别 1 为1,2 为2,3 为3,4 为bB,5 为4,6 为@ 987654337@ 或E。 (详情请阅读choice /?)。那么

...
if %errorlevel%==3 goto :back   REM this was a "3"
if %errorlevel%==4 goto :back   REM this was a "B"
...

“另外,我可以用note来做吗,所以当我选择FFox时会有消息“你选择了FFox”等?”

是的。只需在每个标签后添加一个echo(参见上面带有:notepad 标签的示例。

【讨论】:

  • 如果他真的在运行 MS-DOS,则没有选择选项。 (诚​​然,他可能不会,但现在仍有模棱两可的余地。)
  • 感谢您的回答,它就在附近,但仍然不是我需要做的。
  • 那么,您需要什么?请edit你的问题
  • 非常感谢斯蒂芬的版本,如果我在开始时添加@,它隐藏“方式”和其他标签,看起来不像是我想要的。再次感谢您,如果我有一些问题,我会再问你一次,如果可能的话。
  • 只需将@echo off 添加为第一行。这将抑制所有行的命令重复。
【解决方案2】:

是的,可以在普通的批处理命令中做到这一点。不过,我建议使用 C++ 或 C 之类的语言来执行此操作...但如果您必须使用批处理,here are a lot of commands 可以使用。

【讨论】:

  • 您会感到惊讶,what's possible,使用普通批处理...
  • 感谢大家的时间和帮助。我检查了命令和 md-dos 示例,但仍然找不到我想要的。另外,我需要在示例中看到它,因为我已经做了很多年了,我终于做了类似的事情,现在我迷路了。感谢您的理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多