windows 如何在批处理文件中从控制台读取输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7879791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read input from console in a batch file?
提问by Nohsib
How do I read input from the console in a batch file? What I am trying to achieve is the functionality of scanf in C. How can I do the same in a batch file?
如何从批处理文件中的控制台读取输入?我想要实现的是 C 中 scanf 的功能。如何在批处理文件中执行相同的操作?
回答by Raymond Chen
The code snippet in the linked proposed duplicate reads user input.
链接的建议副本中的代码片段读取用户输入。
ECHO A current build of Test Harness exists.
set /p delBuild=Delete preexisting build [y/n]?:
The user can type as many letters as they want, and it will go into the delBuild variable.
用户可以输入任意数量的字母,它将进入 delBuild 变量。
回答by TankorSmash
If you're just quickly looking to keep a cmd instance open instead of exiting immediately, simply doing the following is enough
如果您只是想快速保持 cmd 实例打开而不是立即退出,只需执行以下操作就足够了
set /p asd="Hit enter to continue"
at the end of your script and it'll keep the window open.
在脚本的末尾,它会让窗口保持打开状态。
Note that this'll set asd
as an environment variable, and can be replaced with anything else.
请注意,这将设置asd
为环境变量,并且可以替换为其他任何内容。
回答by Richard Williams
In addition to the existing answer it is possible to set a default option as follows:
除了现有的答案,还可以设置一个默认选项,如下所示:
echo off
ECHO A current build of Test Harness exists.
set delBuild=n
set /p delBuild=Delete preexisting build [y/n] (default - %delBuild%)?:
This allows users to simply hit "Enter" if they want to enter the default.
这允许用户如果想输入默认值,只需点击“Enter”即可。