Animated loading screen

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
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...


Posts: 114
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 (
	echo Currently working: %%a...
echo done
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 (
	call set "bar=%%bar%%#"
	call echo %%bar%%
	ping localhost -n 1 >nul
echo done

Posts: 3629
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

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

echo %n%

exit /b
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

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

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.


Post Reply