Shane Smiskol
Shane Smiskol

Reputation: 966

How to replace last line in Windows batch code?

I am trying to make a text-based game, and when the user enters something, I want the computer's character to have some kind of thinking animation.

I've come up with this: [. ] [.. ] [...]

Only problem is, I want it all to be on one line, so it's like an actual animation. I've successfully recreated this without removed the text already echo'd, but it requires that you cls, then send all that was on the screen before, three times. Obviously this takes too much space and is pretty inefficient.

I found a code that changes the last like without clearing all that was before it,

@echo off
echo This won't disappear!
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
)

It counts down without erasing the echo before, but I do not understand how to adapt it to add periods to my string... Does anyone know a way to do this? Thanks!

Upvotes: 0

Views: 884

Answers (1)

SomethingDark
SomethingDark

Reputation: 14370

You were almost there with the code you found; you just need to use your own text instead of somebody else's.

for /L %%A in (1,1,3) do (
    set /p "=."<nul 
    ping -n 2 localhost >nul
)

If you want to put the periods inside of brackets, you can use this:

for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A

set /p "=[   ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%%BS%%BS%.  ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%%BS%. ]"<nul
ping -n 2 localhost >nul
set /p "=%BS%%BS%.]"<nul
ping -n 2 localhost >nul

Upvotes: 1

Related Questions