是/否批处理文件 [英] yes/no batch file

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

问题描述

在我的批处理文件中,我想问用户一个问题.

In my batch file I want to ask the user a question.

我写了以下内容:

SET /P ANSWER=Click Y to continue or N to stop (Y/N)

但是我收到的消息没有最后一个).

but I get the message without the last ).

有人知道为什么吗?

谢谢!

推荐答案

因为您正在括号内的提示中使用该提示,例如

Because you're using that prompt within a parenthesised block, e.g.

if ... (
  ...
  set /P ANSWER=Blah (Y/N)
)

for %%x in (...) do (
  ...
  set /P ANSWER=Blah (Y/N)
)

在这种情况下,您必须转义右括号:

You have to escape the closing parenthesis in that case:

SET /P ANSWER=Click Y to continue or N to stop (Y/N^)

或引用整个参数:

SET /P "ANSWER=Click Y to continue or N to stop (Y/N)"

否则它将关闭该块.而且,如果在结束括号后还有其他内容,则会出现语法错误.

otherwise it closes the block. And if you had anything after that closing parenthesis you'd get a syntax error.

一种更简单的操作方法可能是choice命令:

An easier method of what you do there would probably be the choice command:

choice /M "Press Y to continue or N to stop" /c YN

然后您可以随后检查错误级别以找出用户的选择:

You can then check the errorlevel afterwards to find out the user's choice:

if errorlevel 255 (
  echo Error
) else if errorlevel 2 (
  echo No.
) else if errorlevel 1 (
  echo Yes.
) else if errorlevel 0 (
  echo Ctrl+C pressed.
)

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

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