如何清除批处理中的选定行而不是整个屏幕?

How to clear selected lines in Batch instead of the whole screen?

当我们使用 pause 时,它显示 "Press any key to continue . . ."。按任意键后,后面的文字仍然存在,剩下的文字出现在它的下方。如果我在 pause 之后使用 cls 整个屏幕被清除。

有什么方法可以在按任意键后只删除"Press any key to continue . . ."吗?另外,如何只清除选定的行而不是清除整个屏幕?

这个问题有多种解决方案,但 batch 没有提供 简单 解决方案。

批处理的主要问题是,您不能使用普通命令将光标向上移动。

1) 您可以使用cls清除屏幕并重新绘制所有必要的数据。

2) 你可以使用外部程序来移动光标(这个程序可以动态创建),比如Move Cursor to 0,0

3) 你可以滥用timeout命令回到第二行 使用 TIMEOUT 命令将光标移动到主屏幕!Aacini

发现的一个非常酷的技巧

4) 使用 Windows 10 可以使用 Ansi-Escape 序列移动光标并获得许多其他效果,但它仅适用于 Win10

无法在 cmd 中删除某些行。但是,我可以提供一个解决方法:

@echo off
rem // Enable delayed expansion which is necessary to output carriage-return characters later:
setlocal EnableDelayedExpansion
rem // Retrieve a carriage-return character:
for /F %%C in ('copy /Z "%~f0" nul') do set "CR=%%C"
rem // Retrieve a back-space character:
for /F %%C in ('echo prompt $H ^| cmd') do set "BS=%%C"
rem // Prepare message text and a string of spaces:
set "STRING=Press any key to continue . . . "
set "SPACES=                                "

rem // Use `set /P` to print message text without trailing line-break (carriage-return + line-feed):
set /P ="%STRING%" < nul
rem // Do `pause` but suppress its message text:
pause > nul
rem /* Use `set /P` to first print an invisible back-space character; this avoids a white-space
rem    character to appear first to `set /P` as it would not output such leading characters;
rem    then print a single carriage-return character to move the cursor to the beginning of the
rem    current line; then a string of spaces onto the previous message text to overwrite and so to
rem    clear it; append a single carriage-return character, so any other text is printed onto it;
rem    since there is no trailing line-break, any further text is printed at the same line: */
set /P ="%BS%!CR!%SPACES%!CR!" < nul
endlocal

您还可以使用一个奇怪的技巧,将一个 TAB 字符和几个 BS 字符组合起来,将光标向上移动任意行数:

@echo off
setlocal EnableDelayedExpansion

rem Get a BS and TAB control characters
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB=   "  &  REM Be sure that TAB variable contains an Ascii 9 character

rem Leave some empty lines and do a PAUSE
echo Three empty lines below + pause
echo/
echo/
echo/
pause

rem Get width of screen buffer, set number of lines to go above
for /F "tokens=2" %%a in ('mode con ^| findstr "Col"') do set /A buffWid=%%a, linesAbove=3

rem Assemble the "go above" control string with the proper number of BSs
set "BSs="
set /A "cntBS = 2 + (buffWid + 7) / 8 * linesAbove"
for /L %%i in (1,1,%cntBS%) do set "BSs=!BSs!!BS!"

rem Move cursor up the desired number of lines
echo %TAB%!BSs!

echo Hello,
echo World
echo This line overwrites the one with PAUSE output

重要提示:您必须确保 set "TAB= " 行有效包含 TAB (Ascii 9) 字符,而不仅仅是空格。

暂停前的输出:

Three empty lines below + pause



Presione una tecla para continuar . . .

... 暂停后:

Three empty lines below + pause

Hello,
World
This line overwrites the one with PAUSE output

在 Windows 8.1

上测试

此方法是由 DosTips 用户 neorobin 发现的,在 this topic 中有完整描述。