How to clear selected lines in Batch instead of th

2019-01-19 08:38发布

问题:

When we use the pause it shows "Press any key to continue . . .". After pressing any key, the latter text remains, and the remaining text appears below it. If I use cls after pause the whole screen gets cleared.

Is there any method to remove only "Press any key to continue . . ." after pressing any key? Also, how do I clear only selected lines instead of clearing the whole screen?

回答1:

There are multipe solutions for this problem, but batch doesn't offer a simple one.

The main problem of batch is, that you can't move the cursor up with normal commands.

1) You can use cls to clear the screen and redraw all necessary data again.

2) You can use an external program to move the cursor (this programs can be created on the fly), like Move Cursor to 0,0

3) You can abuse the timeout command to go back to the second line Move cursor to screen home with TIMEOUT command!, a very cool trick discovered by Aacini

4) With Windows 10 you can use Ansi-Escape sequences to move the cursor and get many other effects, but it will work only for Win10



回答2:

You may also use a strange trick that combines a TAB character plus several BS ones to move the cursor up any number of lines:

@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

IMPORTANT: You must be sure that the line set "TAB= " effectively contains a TAB (Ascii 9) character, not just spaces.

Output before the PAUSE:

Three empty lines below + pause



Presione una tecla para continuar . . .

... and after the PAUSE:

Three empty lines below + pause

Hello,
World
This line overwrites the one with PAUSE output

Tested on Windows 8.1

This method was discovered by DosTips user neorobin and is fully described at this topic.



回答3:

It is not possible in cmd to delete certain lines. However, I can provide a work-around:

@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