Animated loading screen

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
infectedw
Posts: 4
Joined: 16 Jun 2019 17:29

Animated loading screen

#1 Post by infectedw » 16 Jun 2019 17:37

Any script witch contains loading animated script? I working on my program in batch and need some sort of loading screen...

Thanks!

IcarusLives
Posts: 175
Joined: 17 Jan 2016 23:55

Re: Animated loading screen

#2 Post by IcarusLives » 17 Jun 2019 13:37

Generally whatever it is that is loading, you can prompt it within the code to do so.

For example...

Code: Select all

@echo off

for /l %%a in (1,1,100) do (
	cls
	echo Currently working: %%a...
)
echo done
pause
This "loading" would be quite fast though. If you're looking for something "PURELY" aesthetic, then that is different. A real loading bar is a visual representation of things being added to your pc or variables being saved, etc.

If you're looking for something that is purely for looks, then you can use the code above, with a slight modification.

Code: Select all

@echo off

for /l %%a in (1,1,20) do (
	cls
	call set "bar=%%bar%%#"
	call echo %%bar%%
	ping localhost -n 1 >nul
)
echo done
pause

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Animated loading screen

#3 Post by aGerman » 20 Jun 2019 12:59

Most of those "loading screens" are just a waste of time because
- either there is nothing to load
- or they are synchronously executed

If you have some code that really takes time and you want to let the user know, then you have to animate it asynchronously while your actual task is executed.

Code: Select all

@echo off &setlocal EnableDelayedExpansion

::main
call :start_spinner
set /a "n=0"
for /l %%i in (1 1 50000) do set /a "n+=1"
call :exit_spinner

echo %n%
pause


::spinner
exit /b
:start_spinner
if defined __spin__ goto spin
set "__spin__=1"
for %%i in (v2Forced vtEnabled cursorHide cursorShow colorYellow colorGreen colorRed colorReset) do set "%%i="

for /f "tokens=3" %%i in ('2^>nul reg query "HKCU\Console" /v "ForceV2"') do set /a "v2Forced=%%i"
if "!v2Forced!" neq "0" for /f "tokens=2 delims=[]" %%i in ('ver') do for /f "tokens=2-4 delims=. " %%j in ("%%i") do (
  if %%j gtr 10 (
    set "vtEnabled=1"
  ) else if %%j equ 10 (
    if %%k gtr 0 (set "vtEnabled=1") else if %%l geq 10586 set "vtEnabled=1"
  )
)
if defined vtEnabled (
  for /f %%i in ('echo prompt $e^|cmd') do set "esc=%%i"
  set "cursorHide=!esc![?25l" &set "cursorShow=!esc![?25h"&set "colorYellow=!esc![33m" &set "colorGreen=!esc![32m" &set "colorRed=!esc![31m" &set "colorReset=!esc![m"
)

for /f %%i in ('copy /z "%~f0" nul') do set "cr=%%i"
for /f %%i in ('echo prompt $h^|cmd') do set "bs=%%i"
>"%temp%\spinner.~tmp" type nul
start /b cmd /c ""%~fs0" spin"
exit /b

:exit_spinner
del "%temp%\spinner.~tmp"
set "__spin__="
>nul ping -n 1 localhost
echo(!cr!  - - -!colorGreen!        Ready        !colorYellow!- - -  !colorReset!!cursorShow!
echo(
exit /b

:spin
echo(!cursorHide!!colorYellow!
for /l %%i in () do for %%j in ("\ | / -" "| / - \" "/ - \ |" "- \ | /") do for /f "tokens=1-4" %%k in (%%j) do (
  <nul set /p "=!bs!!cr!  %%k %%l %%m!colorRed!  Please wait . . .  !colorYellow!%%l %%m %%n  "
  >nul ping -n 1 localhost
  if not exist "%temp%\spinner.~tmp" exit
)
In the main code I use a loop that counts its iterations in variable n. It's just a useless example for a task that takes some time and you have to replace it with your own code. However, the spinner is displayed as long as the loop runs. That means both the spinner and the loop run in parallel.

Steffen

infectedw
Posts: 4
Joined: 16 Jun 2019 17:29

Re: Animated loading screen

#4 Post by infectedw » 21 Jul 2019 05:18

Thanks aGerman this will work awesome!

bt.player
Posts: 1
Joined: 06 Jul 2019 11:40

Re: Animated loading screen

#5 Post by bt.player » 26 Jul 2019 14:08

You can using this.

Code: Select all

@echo off

for /l %%a in (1,1,100) do echo|set /p="#"
echo.done.
pause>nul
Last edited by aGerman on 27 Jul 2019 14:31, edited 1 time in total.
Reason: code tags

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Animated loading screen

#6 Post by Eureka! » 27 Jul 2019 15:53

bt.player wrote:
26 Jul 2019 14:08
You can using this.
[...]
Thank you !!
Very elegant (and looking deceivingly simple ...)

bakemonogatari
Posts: 21
Joined: 08 Jul 2019 05:22

Re: Animated loading screen

#7 Post by bakemonogatari » 02 Aug 2019 14:47

aGerman wrote:
20 Jun 2019 12:59

In the main code I use a loop that counts its iterations in variable n. It's just a useless example for a task that takes some time and you have to replace it with your own code. However, the spinner is displayed as long as the loop runs. That means both the spinner and the loop run in parallel.

Steffen
that's pretty cool.

MyFile.bat launches diskmgmt.msc, then MyFile.bat waits as long as diskmgmt.msc stays running

Code: Select all

title MyFile.bat
set process=mmc.exe
set "ProcessName=disk management"

:LOOPMMC
TASKLIST | FINDSTR /i %process% >nul 2>&1 || goto Next
echo %ProcessName% is still running...
timeout /T 5 /Nobreak >nul 2>&1
goto LOOPMMC

:Next
I would like the spinner to run in MyFile.bat as long as mmc.exe is running, how to adapt that?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Animated loading screen

#8 Post by aGerman » 04 Aug 2019 02:23

I hoped I was clear enough:
In the main code I use a loop that counts its iterations in variable n. It's just a useless example for a task that takes some time and you have to replace it with your own code.
What did you try and what didn't work?

Steffen

bakemonogatari
Posts: 21
Joined: 08 Jul 2019 05:22

Re: Animated loading screen

#9 Post by bakemonogatari » 04 Aug 2019 11:02

it was an infinite loop ... but I could do what I wanted with another loading code so it's ok

Post Reply