更改批处理脚本中暂停命令的消息

发布于 2025-01-04 19:40:29 字数 106 浏览 1 评论 0原文

我正在编写一个 Windows 批处理脚本。默认情况下,pause 命令将暂停脚本并显示文本“按任意键继续...”。

如何修改此文本以向用户显示我自己的文本?

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

How do I modify this text to display my own text to the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

娇柔作态 2025-01-11 19:40:30

从 Windows 2000(所以不是 XP)开始,您可以使用 choice 命令,该命令比 pauseset /p 功能强大得多。

除了能够指定您自己的提示文本之外,您还可以要求用户键入特定的键,并且仅键入该键,而不是仅限于并要求按下 [Enter]

此外,您还可以提示用户按多个不同的键,并根据按下的键采取操作。

输入 choice /? 获取帮助,或在此处阅读更多相关信息:维基百科:选择(命令)

Starting from Windows 2000 (so, not XP) you can use the choice command which is far more powerful than either pause or 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.

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)

意犹 2025-01-11 19:40:30

因为我没有看到它的建议,而且互联网上的每只狗都尝试过,但我除外。

这可能无法在 XP 下工作,按照 OP 的要求,但对于任何较新的系统(如我当前的 Win7)都有效:-)

肮脏版本(无消息) |keys ON 不执行任何操作,但阻止按任意键...

timeout -1|Keys ON

可以用于之后的消息
@timeout -1|Keys ON&echo I'm back

这将暂停并显示响应,直到接受,然后返回到提示/下一个命令行

@timeout -1|Echo Hello Padawan %userprofile:~9%, Press a key, you can! 
:on any keys
@echo Welcome to the dark side

在此处输入图像描述

或将其提升为第n个

@color 04&echo/&timeout -1|set /p="Hello %userprofile:~9%, you may press a key to proceed..."&color&echo  Continuing with process...

输入图片此处描述

Since I dont see it suggested and every dog on the internet has had a go, but me.

This may not have worked under XP, as required by OP, but is valid for any newer system like my current Win7 :-)

Dirty version (no message) |keys ON does nothing, but block the press any key ...

timeout -1|Keys ON

can be used for message after
@timeout -1|Keys ON&echo I'm back

This one will pause and show a response until accepted then return to prompt/next cmd line

@timeout -1|Echo Hello Padawan %userprofile:~9%, Press a key, you can! 
:on any keys
@echo Welcome to the dark side

enter image description here

or boost that to the nth

@color 04&echo/&timeout -1|set /p="Hello %userprofile:~9%, you may press a key to proceed..."&color&echo  Continuing with process...

enter image description here

清浅ˋ旧时光 2025-01-11 19:40:30

你可以这样做!

@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

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
浮萍、无处依 2025-01-11 19:40:30
pause|echo text you want displayed

图片cmd 暂停自定义文本

pause|echo text you want displayed

Picture of cmd pause custom text

献世佛 2025-01-11 19:40:29

您可以使用以下命令隐藏 pause 命令中的文本:

pause >nul

然后您可以回显您自己的消息以告诉用户它已暂停:

echo批处理文件已暂停

因此完整的脚本可能如下所示:

@echo off
echo Hello World!
echo The batch file has paused
pause >nul

You could hide the text from the pause command by using this:

pause >nul

Then you could echo your own message to tell the user it 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
愿得七秒忆 2025-01-11 19:40:29

这是一句单行话

  pause>nul|set/p =any key to exit ...

,稍微短一些(少了一个“nul”)
比阿西尼的解决方案:

  set/p<nul =any key to exit ...&pause>nul

并且,首先是“暂停”,我认为
意图是什么就更清楚了。

然而,两者

  cursor stays on the same line  
  ANY key works, not just 'enter'  

都不如假设的那么好,

  pause/t any key to exit ...

很难相信“暂停”仍然存在
35 年没有这种能力;-)

带“echo”的解决方案可能会产生不良后果
尾随换行符,
但确实提供多行文本:

  Pause>nul|(echo  All your bases &echo  are belong to us &echo Press any key to die...)

bv

Here's a one-liner

  pause>nul|set/p =any key to exit ...

It's slightly shorter (one less 'nul')
than Aacini's solution:

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

The solutions with 'echo' have the possibly undesirable
trailing new-line,
but do provide for multiple lines of text:

  Pause>nul|(echo  All your bases &echo  are belong to us &echo Press any key to die...)

bv

强者自强 2025-01-11 19:40:29

“这并不是我想要的,我想知道是否有一种方法可以真正更改 Pause 命令的输出文本,而不仅仅是解决方法。” –哈希姆

是的,你可以!但必须注意,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.

您现在可以运行 CMD .EXE 获取修改后的暂停消息。我对此过程进行了测试:

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

请阅读此答案的每个人注意:

注意 1:请不要发表评论说永远不要修改 CMD.EXE 文件!我也这么认为。我刚刚发布了这个答案,以便 OP 意识到他真正要求的是什么...

注 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                 & ( ) [ ] { } ^ = ; ! % ' + , ` ~               

"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

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 XVI32 program via these steps:

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:

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

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:

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                 & ( ) [ ] { } ^ = ; ! % ' + , ` ~               
迷爱 2025-01-11 19:40:29

无法更改 pause 命令的文本。不过,您可能想查看 choice 命令。您可以更改它打印的文本。唯一的缺点是您需要提供可接受的字符列表。

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.

静谧 2025-01-11 19:40:29

已经有很多解决方案了。另一种有意义的变体:

echo Hit any key to continue...&pause>nul

Already many solutions. Another variation that makes some sense:

echo Hit any key to continue...&pause>nul
旧时光的容颜 2025-01-11 19:40:29

这是另一个技巧

Pause. >nul | echo.  Press something to continue

Here’s another trick

Pause. >nul | echo.  Press something to continue
一曲爱恨情仇 2025-01-11 19:40:29

另一个解决方案是这样的,

SET /P =Press enter to return to the menu . . . 
GOTO :menu

这样做的好处是光标与消息保持在同一行,就像使用PAUSE命令一样。

缺点是它只听回车键。

Another dirty solution 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.

The downside is that it only listens to the enter key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文