如何在 CMD 批处理文件中的暂停命令之间留出余量? [英] How to leave margin between pause command in CMD batch file?

查看:42
本文介绍了如何在 CMD 批处理文件中的暂停命令之间留出余量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么命令可以在暂停命令和窗口边框之间添加边距或空格吗?

Any command to add margin or space between pause command and the window borders?

推荐答案

使用 set/P 可以输出您选择的文本,前导和尾随空格需要等待 在同一行使用pause进行任何按键操作.

It is possible with set /P to output a text of your choice with leading and trailing spaces as wanted with waiting on same line for any key press using pause.

代码如下:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set /P "MyDummyVar=*   Please press a key to continue ... " <nul & pause >nul & echo(
endlocal

注意: 提示文本不能以前导普通空格(代码值十进制 32,十六进制 20)、水平制表符(代码值十进制 9,十六进制 09)或不间断空格开头当前代码页(代码值 255,十六进制 FF,用于 OEM 代码页,例如 437850,代码值 160,十六进制.A0 用于 ANSI 代码页,例如 Windows-1252).命令 SET 在打印提示文本之前删除所有前导空格.

ATTENTION: The prompt text cannot start with leading normal spaces (code value decimal 32, hexadecimal 20), horizontal tabs (code value decimal 9, hex. 09) or non-breaking spaces according to current code page (code value 255, hex. FF for OEM code pages like 437 or 850, code value 160, hex. A0 for ANSI code pages like Windows-1252). The command SET removes all leading whitespaces before printing the prompt text.

发布的解决方案使用以星号开头的提示文本,第二个字符是 退格 十进制代码值为 8 的控制字符.也可以使用与星号不同的非空白字符作为第一个字符.

The posted solution uses a prompt text starting with an asterisk and the second character is a backspace control character with decimal code value 8. There can be used also a different non-whitespace character than the asterisk as first character.

注意:用于查看此页面的浏览器可能根本不显示*后的退格控制符,但贴出的代码在后有一个退格控制符>*.如果退格控制字符也与三个普通空格和第三个命令行的所有其他字符一起复制,这取决于所使用的浏览器.这取决于所使用的文本编辑器如何将退格字符粘贴到批处理文件中,并由浏览器真正复制为退格符以及所使用的字体,如果显示的话,退格控制字符如何在文本编辑器窗口中显示.

Note: The browser used to view this page might not display the backspace control character after * at all, but the posted code has a backspace control character after *. It depends on the used browser if the backspace control character is copied also with the three normal spaces and all the other characters of third command line. It depends on the used text editor how the backspace character is pasted into the batch file on really copied as backspace by the browser and on the used font how the backspace control character is displayed in the text editor window if displayed at all.

此解决方案的灵感来自 Windows 批处理:没有换行符的回显dbenham 和 DosTips 论坛帖子 输出没有换行符的文本,即使有前导空格或 =jeb 编写.

This solution is inspired by Windows batch: echo without new line written by dbenham and the DosTips forum post Output text without linefeed, even with leading space or = written by jeb.

<代码>&echo( 在命令行的末尾是下一行的必要输出,而不是与提示文本在同一行,因为 pause 输出的换行符也被重定向到设备NUL.

& echo( at end of the command line is necessary to have the next output on next line and not on same line as the prompt text because of the newline characters output by pause are redirected also to device NUL.

适用于 Windows Vista 和更新的 Windows 版本以及 Windows Server 2003 和更新的 Windows Server 版本的另一个解决方案是:

Another solution for Windows Vista and newer Windows versions and Windows Server 2003 and newer Windows Server versions is:

%SystemRoot%\System32\choice.exe /N /M "   Please press a key to continue ..." <nul 2>nul & pause >nul & echo(

注意: choice.exe 默认在 Windows XP 上不可用.可以将 Windows Server 2003 的 choice.exe 复制到 Windows XP 的 Windows 系统目录中,使其也可以在运行 Windows XP 的计算机上使用.换句话说,此解决方案不应用于批处理文件中,批处理文件也应在 Windows XP 或 Windows 2000 上运行.

Note: choice.exe is by default not available on Windows XP. choice.exe of Windows Server 2003 can be copied into the Windows system directory of Windows XP to make it available also on a computer running Windows XP. In other words this solution should not be used in batch files which should run also on Windows XP or Windows 2000.

在此示例中使用了三个带有 OEM 代码值的不间断空格CHOICE 删除的前导普通空格和水平制表符相比,命令 CHOICE 不会删除的提示文本开头的 255(十六进制 FF).

There are used three non-breaking spaces in this example with OEM code value 255 (hexadecimal FF) at beginning of the prompt text which the command CHOICE does not remove in comparison to leading normal spaces and horizontal tabs which are removed by CHOICE.

CHOICE 从提示文本中删除尾随的普通空格/水平制表符后,输出的提示文本总是附加一个尾随空格.CHOICE 不会删除尾随的不间断空格,并且可以附加在提示文本的末尾以获得更多输出尾随空格.

CHOICE outputs the prompt text always with one trailing space appended after removing trailing normal spaces/horizontal tabs from prompt text. Trailing non-breaking spaces are not removed by CHOICE and can be appended at end of the prompt text for even more output trailing spaces.

设备 NUL 用于标准输入流,导致错误消息:

The device NUL is used for the standard input stream which results in the error message:

错误:文件为空或不包含有效选项.

ERROR: The file is either empty or does not contain the valid choices.

此错误消息使用 2>nul 重定向到设备 NUL 以抑制它.

This error message is redirected with 2>nul to device NUL to suppress it.

结果是choice.exe在打印提示文本后立即自行终止,然后由cmd.exe执行内部命令pause>.

The result is that choice.exe immediately terminates itself after printing the prompt text and so internal command pause is executed next by cmd.exe.

最好使用此解决方案来获取 DosTips 中 Compo 建议的当前使用的代码页数表单发布 [信息] 保存当前代码页,接下来将 OEM 代码页 437 设置为用于使用不间断空格对提示文本进行编码,输出提示并恢复初始代码页.这可以通过以下代码完成:

It would be best on using this solution to get the number of currently used code page as suggested by Compo in DosTips form post [Info] Saving current codepage, next set OEM code page 437 as used to encode the prompt text with the non-breaking spaces, output the prompt and restore the initial code page. This can be done with the following code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=*" %%G in ('%SystemRoot%\System32\chcp.com') do for %%H in (%%G) do set "CodePage=%%~nH"
%SystemRoot%\System32\chcp.com 437 >nul
%SystemRoot%\System32\choice.exe /N /M "   Please press a key to continue ..." <nul 2>nul & pause >nul & echo(
%SystemRoot%\System32\chcp.com %CodePage% >nul
endlocal


PS:我一直不明白为什么命令 PAUSE 不支持可选的参数字符串,该字符串被解释为提示文本而不是内置提示文本.我在 2002 年编写了一个非常小的 32 位控制台应用程序作为 pause 的替代品,我称之为 batpause.exe,它类似于 PAUSE 并支持可选的参数字符串解释为小应用程序输出的提示文本,而不是内置的默认提示文本.


PS: I have never understood why command PAUSE does not support optionally an argument string which is interpreted as prompt text instead of the built-in prompt text. I wrote in year 2002 a very small 32-bit console application as replacement for pause which I called batpause.exe which is like PAUSE and supports optionally an argument string interpreted as prompt text output by the small application instead of built-in default prompt text.

这篇关于如何在 CMD 批处理文件中的暂停命令之间留出余量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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