Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File progress spinning wheel

I have been trying for days and can seem to get this to work. I found an example but it uses a (CryEcho) which will not work. I just wanted to add this to let the user know something is going on while im pinging IP addresses. I did find some code on here but it was confusing to me since im just starting to mess around with batch files for fun.

Anyway, I wanted to have something that used something like the example below but with text like (Waiting...[spinner]). Thanks!

@echo off
setlocal

set COUNT=0
set MAXCOUNT=10
set SECONDS=1

:LOOP
title "\"
call :WAIT
title "|"
call :WAIT
title "/"
call :WAIT
title "-"
if /i "%COUNT%" equ "%MAXCOUNT%" goto :EXIT
set /a count+=1
echo %COUNT%
goto :LOOP

:WAIT
ping -n %SECONDS% 127.0.0.1 > nul
ping -n %SECONDS% 127.0.0.1 > nul
goto :EOF

:EXIT
title FIN!
endlocal

AND I found this code as well:

@echo off
rem Example showing how to use CryEcho to produce a spinning wheel to show activity.

CryEcho Working ... 
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork
call :SpinAlive
call :DoSomeWork

cryecho \s\nFinished.

goto :eof

:DoSomeWork
ping -n 1 localhost > nul
goto :eof

:SpinAlive
if "%Spinner%" == "2" (cryecho \\\b) 
if "%Spinner%" == "3" (cryecho -q "|"\b) 
if "%Spinner%" == "4" (cryecho /\b set Spinner=0) else (cryecho -\b set Spinner=1)
set /A Spinner=%Spinner%+1
goto :eof
like image 484
SoggyCashew Avatar asked Dec 27 '22 10:12

SoggyCashew


1 Answers

The main trick is here to move the cursor back on the same line.
This can be done with a carriage return or a backspace characters.

@echo off
setlocal EnableDelayedExpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

FOR /L %%n in (1,1,10) DO (
    call :spinner
    ping localhost -n 2 > nul
)
exit /b

:spinner
set /a "spinner=(spinner + 1) %% 4"
set "spinChars=\|/-"
<nul set /p ".=Waiting !spinChars:~%spinner%,1!!CR!"
exit /b

The <CR> character is created from the output of copy /z.
The output is done by set /p, as this omits the output of CR/LF at the end.
The !CR! is output each time to force the cursor to move back to the first column.
But as Win7 and Vista removes all whitespaces and also CR from the leading output of set /p, it's placed at the end.

like image 144
jeb Avatar answered Jan 04 '23 20:01

jeb