user1440897
user1440897

Reputation:

Is it possible to edit a line already outputted in Windows batch?

So, I am currently making a "loading screen", and to possibly save some space in my coding, I want to know if you could edit a line already outputted. I would have maybe a bracket [] as one stage of loading, so would it be possible to put one bracket, then wait and see if user presses C (for continue) for 1-2 seconds, and if not go to the next stage ([][])? I currently have a script where [] is set as load and for every stage, I do CLS and then echo %LOAD%[].


In addition, what if I just want to do a status update on a line, such as:

Checking status...
Loading server...

and then

Checking status... OK
Loading server... done

Bonus points if you can find me a character like that is compatible with Batch.


Upvotes: 5

Views: 165

Answers (2)

jeb
jeb

Reputation: 82337

Creating a blockchar like can be done with

setlocal EnableDelayedExpansion
for /F "usebackq tokens=1" %%c in (
    `forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0xde"`) do (
    set BlockChar=%%c
)
echo %BlockChar%

Thanks to dbenham Generate nearly any character, including TAB, from batch

Upvotes: 2

jeb
jeb

Reputation: 82337

You can ommit the CLS and recreating the full screen with the help of set /p, as set /p doesn't output a newline, you can append text.

Normally set /p is for assigning text to a variable inputted by a user, but if you use redirection from NUL it simply outputs text.

@echo off

for /L %%n in (1 1 5 ) do (
  <nul set /p ".=[]"
  ping -n 2 localhost > nul
)
echo(
echo The end

The status update you asked for, can be handled the same way, as it only appends something to the line.
If you want to change parts of the line or the complete line, you need to move the cursor back or to the beginning of the line.
Both can be done with the backspace character or the carriage return character.

This is a sample which counts on a fixed screen location

setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
for /L %%n in (1 1 1000) do (
  <nul set /p ".=%%n!CR!"
)

Upvotes: 5

Related Questions