So I was making a batch file that allows to make other people think you can hack, and in one part it says in the CMD window Starting hack in 10... Starting hack in 9... etc. What I want to do is to instead of appearing on different lines I want the lines to change by deleting the last line that's showing on the CMD window. here's how the code for that part of the program looks like right now
echo Starting hack in 10...
timeout /t 1
echo Starting hack in 9...
timeout /t 1
echo Starting hack in 8...
timeout /t 1
echo Starting hack in 7...
timeout /t 1
echo Starting hack in 6...
timeout /t 1
echo Starting hack in 5...
timeout /t 1
echo Starting hack in 4...
timeout /t 1
echo Starting hack in 3...
timeout /t 1
echo Starting hack in 2...
timeout /t 1
echo Starting hack in 1...
timeout /t 1
tree
echo Hacking completed!
pause
I was also searching for a way to do it on stack overflow but I didn't find anything helpful so I created this account JUST TO MAKE THIS QUESTION. Please I need answers.
CodePudding user response:
Use Cls
echo Starting hack in 10... timeout /t 1 Cls
CodePudding user response:
To overwrite the last line, all you have to do is a carriage return instead of a linefeed (carriage return plus line feed).
I'm using a subroutine here, params are a message string (quoted because of spaces) and a number (counter).
@echo off
setlocal EnableDelayedExpansion
REM create a CariageReturn:
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
echo Welcome to %~n0
echo/
call :spinner "Starting in" 10
echo It has now started.
echo insert payload here
goto :eof
:spinner
set counter=%2
:spinnerloop
<nul set /p ".=%~1 %counter% !CR!"
set /a counter-=1
timeout 1 >nul
if %counter% geq 0 goto :spinnerloop
Make sure, the first echo
after the call
is long enough to overwrite the complete message plus counter (add enough spaces if necessary) or echo
a new line (echo/
) if you want to keep the last message on screen.