windows 在批处理脚本中更改暂停命令的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9258822/
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
Change output of pause command in batch script
提问by Hashim
I'm writing a Windows batch script. By default, the pause
command will pause the script and display the text "Press any key to continue...".
我正在编写 Windows 批处理脚本。默认情况下,该pause
命令将暂停脚本并显示文本"Press any key to continue..."。
How do I modify this text to display my own text to the user?
如何修改此文本以向用户显示我自己的文本?
Thanks. :)
谢谢。:)
回答by Bali C
You could hide the text from the pause
command by using this:
您可以pause
使用以下命令隐藏命令中的文本:
pause >nul
pause >nul
Then you could echo your own message to tell the user it has paused:
然后你可以回显你自己的消息告诉用户它已经暂停:
echo The batch file has paused
echo The batch file has paused
So the full script might look like this:
所以完整的脚本可能如下所示:
@echo off
echo Hello World!
echo The batch file has paused
pause >nul
Hope this helps :)
希望这可以帮助 :)
回答by bob vance
Here's a one-liner
这是一个单线
pause>nul|set/p =any key to exit ...
It's slightly shorter (one less 'nul') than Aacini's solution:
它比 Aacini 的解决方案略短(少一个 'nul'):
set/p<nul =any key to exit ...&pause>nul
and, with the 'pause' first, I think that it's a little clearer what the intent is.
而且,首先是“暂停”,我认为意图是更清楚一点。
With both
既
cursor stays on the same line
ANY key works, not just 'enter'
Neither are as good, however, as the hypothetical
然而,两者都没有假设的那么好
pause/t any key to exit ...
It's hard to believe that 'pause' has survived 35 years without this ability ;-)
很难相信如果没有这种能力,“暂停”还能存活 35 年;-)
The solutions with 'echo' have the possibly undesirable
trailing new-line,
but do provide for multiple lines of text:
带有 'echo' 的解决方案可能会有不受欢迎的尾随换行符,
但确实提供了多行文本:
Pause>nul|(echo All your bases &echo are belong to us &echo Press any key to die...)
bv
bv
回答by Aacini
"Not really what I was looking for, I was wondering whether there was a way of actually changing the output text of the Pause command, not just a workaround to it." – Hashim
“这不是我真正想要的,我想知道是否有一种方法可以实际更改 Pause 命令的输出文本,而不仅仅是一种解决方法。” – 哈希姆
Yes, you can! But you must be aware that PAUSE is an internal command of CMD.EXE program, so, to modify the message that PAUSE show, you must modify CMD.EXE file. To do that, you must use an editor that may modify binary files. I used XVI32program via these steps:
是的你可以!但是你必须知道 PAUSE 是 CMD.EXE 程序的内部命令,所以,要修改 PAUSE 显示的信息,你必须修改 CMD.EXE 文件。为此,您必须使用可以修改二进制文件的编辑器。我通过以下步骤使用了XVI32程序:
1- Copy CMD.EXE file to a new folder created for this purpose: COPY %COMSPEC%
2- Edit the copy of CMD.EXE with XVI32.EXE program:
2.1- Locate the message you want. Messages are stored in 16-bits elements with the high byte equal zero. To locate a message:
2.1.1- In Search> Find> Text string> enter the message you want.
2.1.2- Convert Text -> Hex
2.1.3- Insert a zero after each letter-value
2.1.4- Press Ok
2.2- Modify the message for the new one. Modify existent letters only and keep zeros in place. Note that you can NOT extend any message.
2.3- End the edition and save the modified file.
You may now run CMD.EXE to get the modified PAUSE message. I made a test of this procedure:
您现在可以运行 CMD.EXE 来获取修改后的 PAUSE 消息。我对这个程序做了一个测试:
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>pause
Press any key to continue . . .
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\DOCUME~1\Antonio\MYDOCU~1\My Webs\XVI32 Hex File Editor
>pause
Oprime una tecla para seguir. .
Notes to everyone that read this answer:
给所有阅读此答案的人的注意事项:
NOTE 1: Please, don't post that comment saying that modifying CMD.EXE file must NEVER be done! I think the same. I just posted this answer so the OP realized what he really asked for...
注意 1:请不要发表评论说永远不要修改 CMD.EXE 文件!我也是这么想。我刚刚发布了这个答案,所以 OP 意识到他真正要求的是什么......
NOTE 2: The reviewing of CMD.EXE file with a text editor, like Notepad, is very interesting. You may see all the internal commands, interesting details (like =ExitCode and =ExitCodeAscii variables), all the error messages, etc. For example, these are the internal commands:
注意 2:使用文本编辑器(如记事本)查看 CMD.EXE 文件非常有趣。您可能会看到所有内部命令、有趣的细节(例如 =ExitCode 和 =ExitCodeAscii 变量)、所有错误消息等。例如,这些是内部命令:
C O L O R T I T L E C H D I R C L S C M D E X T V E R S I O N
D E F I N E D C O P Y P A T H P R O M P T P U S H D P O P D
A S S O C F T Y P E D A T E D E L D I R E C H O E N D L O C A L
E R A S E E R R O R L E V E L E X I T E X I S T B R E A K F O R
G O T O I F K E Y S M K D I R M D N O T P A U S E R D
R E M M O V E R E N A M E R E N R M D I R S E T S E T L O C A L
S H I F T S T A R T T I M E T Y P E V E R I F Y V E R V O L = , ; + / [ ] " : . \
P A T H E X T P A T H P R O M P T
F O R / ? I F / ? R E M / ?
% s % s
% s
/ A / P : E O F
f d p n x s a t z D O
/ L / D / F / R I N E L S E ( % s ) % s % s % s % s
% c % c % s % s & ( ) [ ] { } ^ = ; ! % ' + , ` ~
回答by Michael
There is no way to change the text of the pause
command. However you might want to look at the choice
command. You can change the text it prints. The only downside is that you need to provide a list of acceptable characters.
无法更改pause
命令的文本。但是,您可能想查看choice
命令。您可以更改它打印的文本。唯一的缺点是您需要提供可接受的字符列表。
回答by Trevi Awater
Another dirtysolution would be something like this,
另一个肮脏的解决方案是这样的,
SET /P =Press enter to return to the menu . . .
GOTO :menu
The benefit of this is that the cursor stays on the same line as the message, just like with the PAUSE
command.
这样做的好处是光标与消息保持在同一行,就像PAUSE
命令一样。
The downside is that it only listens to the enter key.
缺点是它只听回车键。
回答by Panikosagros
Here's another trick
这是另一个技巧
Pause. >nul | echo. Press something to continue
回答by Mike Nakis
Starting from Windows 2000 (so, not XP) you can use the choice
command which is far more powerful than either pause
or set /p
.
从 Windows 2000(因此,不是 XP)开始,您可以使用choice
比pause
或set /p
.
Besides being able to specify your own prompt text, you can also require the user to type a specific key, and only that key, instead of being limited to, and requiring, [Enter]
to be pressed.
除了能够指定您自己的提示文本之外,您还可以要求用户键入特定的键,并且仅该键,而不是被限制和要求[Enter]
被按下。
Also, you can prompt the user to press a number of different keys and take action based on which key was pressed.
此外,您可以提示用户按下多个不同的键,并根据按下的键采取行动。
Type choice /?
for help, or read more about it here: Wikipedia: choice (command)
键入choice /?
寻求帮助,或在这里阅读更多关于它:维基百科:选择(命令)
回答by Super461
You could do it like this!
你可以这样做!
@echo off
echo Hello World!
echo:
echo Press 1 to continue
set /p letter=
if %letter% == 1 goto a ;or instead of goto you could write start and the file or website you want to start
pause >nul
:a
cls
echo BYE NOW!
pause >nul