Galactic Civilizations 3
Display a progress bar in a batch script

Display a progress bar in a batch script

Category: Programming.
Posted by on the 02/01/2014.
Last update on the 05/01/2014.

Description

This tutorial explains how to display a progress bar (also called loading bar) in a batch script under Windows.

Introduction

This tutorial is intended to people that uses Windows XP©, Windows Vista©, Windows 7© or Windows 8 and it does not require any prerequisites.
This tutorial can be realized with a simple text editor like « Notepad ».

Display a simple progress bar

Our first progress bar simply consist to display a sequence of points « . ». To do that, we will use the command « echo|set /p » to display a point without carriage return.

Here is an example that shows how to use this command:

@echo off
set FINISH=0
set i=0
echo.
:NEW_STEP
REM We display a point without carriage return.
echo|set /p=.

REM Operation to execute
ping 123.45.67.89 -n 1 -w 500 > nul

set /a i = i + 1
if /i %i% leq 10 goto NEW_STEP
echo.
pause

The script executes 10 times the command « ping 123.45.67.89 -n 1 -w 500 > nul » and adds, at each time, a point to the progress bar.

Here is another example that displays a progress bar:

@echo off
REM Operation to execute
REM ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP

PAUSE>NUL
goto :EOF
:NEW_STEP
REM We display a point without carriage return.
echo|set /p=.
exit /b

Here, each time that we call the command « call :NEW_STEP » a point is added to the progress bar.

NB: The command « call » allows calling another batch script without stopping the execution of the current script.

Display a progress bar with a progression percentage

Now, we will display the same progress bar as in the previous point, but we will also display the percentage of progression of the program in the title of the window.

To do that, we will use the command « title ».

title allows to modify the title of the window where the script is executed.

Here is an example that shows how to use the command:

@echo off
REM Operation to execute
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 30
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 60
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 100

PAUSE>NUL
goto :EOF
:NEW_STEP
REM We display a point without carriage return.
echo|set /p=.
title %1%%% Completed
exit /b

The script calls 3 times « call :NEW_STEP » and gives the progression percentage. This one is then displayed in the title of the window.

Display a complete progress bar

Now, we will improve the script in order to display a real progress bar that change on the same line.

The progress bar will look like this:

[#### ]

Here is the script that allows displaying the progress bar:

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

echo My progress bar:
REM Operation to execute
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 30
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 60
ping 123.45.67.89 -n 1 -w 500 > nul
call :NEW_STEP 100

PAUSE>NUL
goto :EOF
:NEW_STEP

set percentage=%1%

REM We draw the progress bar.
set "tmp=["
:UP_BAR
if /i !i! leq !percentage! (
set "tmp=!tmp!#"
) else (
set "tmp=!tmp! "
)
set /a i = i + 5
if /i !i! leq 100 goto :UP_BAR
set "tmp=!tmp!]"

REM We display the progress bar
if !percentage! equ 100 (
echo !tmp!
) else (
<nul set /p ".=!tmp!!CR!"
)

title !percentage!%% Completed
exit /b
endlocal

The script calls 3 times « call :NEW_STEP » and gives the progression percentage. This one is then displayed in the title of the window and the progress bar is displayed on the window.

You like this tutorial ?
Like this website in order to promote it on facebook !

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo