0

I was trying to find a way to clear (CLS) a single line (see This Question), but found that the solutions, which involved moving the cursor, required extra software or modifications to files, which is not ideal.

However, I later noticed that the TIMEOUT command actually moves the cursor, which can be demonstrated with the following code:

@ECHO off
ECHO Notice how the cursor moves to the number.
ECHO Press [Ctrl]+[C] for an interesting result.
PAUSE
ECHO(
TIMEOUT 99999
PAUSE

If Ctrl+C is pressed after a couple of seconds, the "Terminate Batch Job" prompt overwrites the TIMEOUT output after the number. Any typing also continues to overwrite it.

What I am looking for, is any way to clear a line, change the line, or move the cursor somewhere, and also anything similar that could be useful. (I know this is a bit vague)


One idea was if it was possible to modify the stdout in such a way that the cursor still moved, it would at least be possible to get a changing number at one point in a custom line.

It might also be possible to asynchronously do both TIMEOUT and ECHO commands to have a similar effect to Ctrl+C.


Redirecting the TIMEOUT stdout to nul gives no LF character. This in conjunction with:

<nul SET /P var=#
    REM echo without a LF character

...Could possibly be used to create progress bars.


Any thoughts, ideas, or other commands with similar effects are welcome.

Community
  • 1
  • 1
NotEnoughData
  • 188
  • 3
  • 8

1 Answers1

0

Using answers from here you can have an increasing number printed.

It is important to insert the backspace character in place of insert_baskspace_here below.

You will need an editor that supports insertion of ASCII characters. I used Ultraedit but Notepad++ works too.

@echo off
setlocal enableextensions enabledelayedexpansion

set BS=insert_baskspace_here

Echo I printed 1 time
set /p "=I printed 2 times" <NUL

for /L %%I in (3,1,9) DO (
  timeout /t 1 > nul
  set /p "=!BS!!BS!!BS!!BS!!BS!!BS!!BS!%%I times" <NUL
)

The output starts as:

I printed 1 time
I printed 2 times

It changes each second and finishes with:

I printed 1 time
I printed 9 times

A few changes and you have a little progress spinner wheel:

@echo off
setlocal enableextensions enabledelayedexpansion

set BS=

set /p "=Processing.../" <NUL

for /L %%J in (0,1,9) DO (
  for %%I in (-,\,^|,/,) DO (
    timeout /t 1 > nul
    set /p "=!BS!%%I" <NUL
  )
)
Community
  • 1
  • 1
Okkenator
  • 1,654
  • 1
  • 15
  • 27
  • Thanks. Great technique, though it doesn't seem to handle lines that are long enough to wrap around very well (but this might not be possible). – NotEnoughData Jul 07 '15 at 22:46
  • Correct, as far as I know it is not possible to make the cursor backspace up a line. – Okkenator Jul 08 '15 at 11:47
  • Maybe other control characters can work/help/have effects. [This site](https://www.cs.tut.fi/~jkorpela/chars/c0.html) has some info on the functions/purposes of control characters. – NotEnoughData Jul 09 '15 at 03:55