10

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?

mythofechelon
  • 3,692
  • 11
  • 37
  • 48
  • [How to overwrite the same line in command output from batch file](https://superuser.com/q/82929/241386) – phuclv Dec 25 '17 at 10:22

4 Answers4

17

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
)
jeb
  • 78,592
  • 17
  • 171
  • 225
  • I've tried this but the section of text at the end remains. Here's a gif to show the result I'm getting. On the latest build of windows. https://i.imgur.com/3aR3JwM.gif – Ste Apr 01 '20 at 13:24
  • 1
    @Ste You need to add some spaces after the seconds, like `...close in %%n seconds !CR!"` – jeb Oct 05 '20 at 19:48
  • Thanks jeb. I should of known that. – Ste Oct 05 '20 at 23:08
4

Try ANSI sequences: http://www.robvanderwoude.com/ansi.php

Burrowing down the links, http://batch.xoo.it/t2238-BG-exe-Utility-for-Batch-Games.htm looks the most promising.

This page sounds like it has useful discussion on controlling/setting console sizes (and other display and buffer size settings). http://www.pcreview.co.uk/forums/change-buffer-size-console-window-can-runas-inherit-console-props-t1468842.html

azhrei
  • 2,303
  • 16
  • 18
  • The ANSI sequences work fine on old DOS machines, but they do not work with the Windows command line. I miss that functionality :( – dbenham Aug 15 '12 at 15:49
  • Okay, thanks. I'll have a good look over both answers and then select an answer. :) – mythofechelon Aug 17 '12 at 14:23
  • It'll work in [windows 10](https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032%28v=vs.85%29.aspx) – phuclv Jul 08 '16 at 03:44
4

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • So there isn't a command line version of marking and copying or anything like that? Or any way of refreshing one line other than my method? :/ – mythofechelon Aug 15 '12 at 23:42
  • No native method, but you can download a utility as I suggested and move the cursor to any position on the screen. At that point you can overwrite what is already there. – dbenham Aug 15 '12 at 23:50
  • Okay, thanks. I'll have a good look over both answers and then select an answer. :) – mythofechelon Aug 17 '12 at 11:20
0

An alternative quick&dirty method of moving the cursor via TIMEOUT:

@echo off
<nul set/p"=5 seconds till i close..."
timeout /t 5 /nobreak >con
echo(i'm closing now...[REPLACE this with lots of spaces]
exit /b

ScriptKidd
  • 803
  • 1
  • 5
  • 19