是/否批处理文件

编程入门 行业动态 更新时间:2024-10-07 16:21:43
本文介绍了是/否批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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. )

更多推荐

是/否批处理文件

本文发布于:2023-11-28 03:10:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!