Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLS (clear) a single line?

Is there any way to CLS a single line of output? I don't believe there are any switches for CLS, so maybe a better question would be:

Is there any way to

  1. retain all previous output for re-use?
    or
  2. capture currently displayed output (like you can by marking and copying)?

I'm just trying to make my scripts a little more user-friendly by having real-time feedback / information, instead of multiple lines with slight changes. The only way I can think of doing this, though, is like this:

@echo off
goto Prep

:Prep
    SET count=5
    SET genericMessage=This window will close

    goto Output

:Output
    IF NOT %count% == -1 (
        cls
        IF %count% == 0 (
            echo %genericMessage% now.
        ) ELSE (
            echo %genericMessage% in %count% seconds.
        )
        SET /A count=%count% - 1
        ping localhost -n 2 >nul
        goto Output
    ) ELSE (
        exit
    )

So, you get this:

enter image description here

The problem with this, though, is that CLS erases all output, when I only want to refresh one line by erasing and re-outputting it.

Anyone have any ideas?

like image 706
mythofechelon Avatar asked Aug 15 '12 15:08

mythofechelon


People also ask

How do I delete a single line in CMD?

There are no keyboard shortcuts to clear the screen in command Prompt, but you cal press ESC to clear the input line and Cntrl + C to move the cursor to the new blank line.

What does CLS mean in batch file?

Cls: The cls command clears the prompt screen.


2 Answers

If you only need to move the cursor in one line (like your sample),
it's possible with a carriage return character.

@echo off
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
for /L %%n in (5 -1 1) do (
  <nul set /P "=This window will close in %%n seconds!CR!"
  ping -n 2 localhost > nul
)
like image 171
jeb Avatar answered Oct 16 '22 19:10

jeb


Unfortunately, there is no native command or utility that repositions your cursor in a Windows command line console.

You will need a 3rd party utility.

Aacini posted a free CursorPos.exe utility on DOSTips. The CurorPos.exe "source" is given as Hex digits. To use the source you will need the HexToBin.bat "compiler".

Browse both threads and you will find a number of utilities you may find useful.

like image 30
dbenham Avatar answered Oct 16 '22 19:10

dbenham