Skip to main content

How to Display a Horizontal Bar Chart in Batch Script

While Batch scripts are primarily used for text processing and automation, you can also use them to create simple, impactful data visualizations. A Horizontal Bar Chart is an excellent way to display disk usage, progress percentages, or comparative statistics (like CPU load) directly in the console without needing a GUI.

In this guide, we will demonstrate how to build a dynamic bar chart using string repetition and ANSI characters.

The Basic Concept

To create a bar chart, we take a numeric value (e.g., 75%) and convert it into a string of symbols (e.g., 37 blocks if scaled to half-width). We then print that string next to a label.

The Padding Loop

Because Batch does not have a "repeat character" function, we use a FOR /L loop to append a block character to a variable until it reaches the desired length.

Method 1: The Simple ASCII Bar

This version uses standard characters (like #) and works on all versions of Windows.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: Maximum bar width in characters (prevents bars from exceeding terminal width)
set "maxBarWidth=50"

:: Space padding for label alignment (must be at least as wide as the longest label)
set "labelPad= "

:: Define the "data" for our chart (Label:Value as percentage)
set "items=Sales:40 Marketing:75 Support:20 Engineering:60"

echo PERFORMANCE DASHBOARD
echo ------------------------------------------

for %%A in (%items%) do (
for /F "tokens=1,2 delims=:" %%a in ("%%A") do (
set "label=%%a!labelPad!"
set "val=%%b"

:: Scale value to fit within terminal width
:: Maps 0-100%% to 0-maxBarWidth characters
set /a "barLen=(val * maxBarWidth) / 100"

:: Generate the bar string
set "bar="
for /L %%i in (1,1,!barLen!) do set "bar=!bar!#"

:: Print padded label and bar
echo !label:~0,15! !bar! ^(!val!%%^)
)
)

echo ------------------------------------------

endlocal
pause

Method 2: The Modern ANSI Block Chart (Cleanest)

On Windows 10 and 11, we can use the "Full Block" character () and ANSI colors to create a much more professional-looking chart.

@echo off
setlocal enabledelayedexpansion

set "labelPad= "
set "disks=C:85 D:45 E:12"

echo DISK CAPACITY MONITOR
echo.

for %%A in (%disks%) do (
for /F "tokens=1,2 delims=:" %%D in ("%%A") do (
set "name=%%D!labelPad!"
set "used=%%E"

:: Scale: divide by 2 to keep bars within terminal width
set /a barWidth=used / 2

set "bar="
for /L %%i in (1,1,!barWidth!) do set "bar=!bar!█"

echo !name:~0,5! [!bar!] !used! GB
)
)

endlocal
pause

Creating a Relative "Progress Bar"

Sometimes you want the bar to be a fixed width (e.g., 20 blocks wide) and show progress within that frame.

@echo off
setlocal enabledelayedexpansion

set "current=150"
set "total=200"
set "barWidth=20"

:: Calculate percentage
set /a "pct=(current * 100) / total"

:: Calculate how many of the blocks should be filled
set /a "fill=(pct * barWidth) / 100"
set /a "empty=barWidth - fill"

set "bar="
for /L %%i in (1,1,%fill%) do set "bar=!bar!█"
for /L %%i in (1,1,%empty%) do set "bar=!bar!░"

echo Progress: [!bar!] %pct%%%

endlocal
pause

Key Components:

  • (Alt+219): The solid block character for the filled portion.
  • (Alt+176): The light shaded block for the empty portion.
  • Scaling: If your data values are very high (like 1000), divide them by a factor (like 10 or 50) so the bar doesn't wrap to the next line.
Note on special characters

The and characters are from the OEM character set (code page 437) used by cmd.exe by default. If your batch file is saved with a different encoding, these characters may not display correctly. Most Windows text editors save .bat files in the correct encoding automatically.

Creating Borders and Headers

To make your table look truly professional, surround it with ASCII borders.

@echo off
setlocal enabledelayedexpansion

set "labelPad= "

echo +----------------------+------------+
echo ^| NAME ^| STATUS ^|
echo +----------------------+------------+

set "n1=System Check!labelPad!"
set "n2=Database Sync!labelPad!"

set "s1=ONLINE!labelPad!"
set "s2=OFFLINE!labelPad!"

echo ^| !n1:~0,20! ^| !s1:~0,10! ^|
echo ^| !n2:~0,20! ^| !s2:~0,10! ^|
echo +----------------------+------------+

endlocal
pause

Best Practices

  1. Label Alignment: Use the fixed-width padding trick (appending spaces and taking a substring) to ensure the start of every bar lines up vertically.
  2. Scale to Fit: Always scale your bar lengths relative to the terminal width. A maxBarWidth of 40–50 characters leaves room for labels and values on an 80-column terminal.
  3. Color Coding: Use Red for "Danger" levels (e.g., Disk > 90%) and Green for "Safe" levels.
  4. Handle Zeros: If a value is 0, for /L %%i in (1,1,0) simply does not iterate, leaving the bar empty. This is correct behavior, but consider printing a placeholder like [empty] if a completely blank bar might confuse users.

Conclusion

Horizontal bar charts are a powerful way to add a layer of visual intelligence to your Batch scripts. Instead of forcing a user to read through numbers, you can show them exactly where the bottlenecks or capacity issues are at a single glance. With a few simple loops and ANSI blocks, your command-line tools can provide dashboard-level insights with zero external dependencies.