Skip to main content

How to Draw a Box or Frame Around Text Output in Batch Script

Simple text output can often get lost in the noise of a busy terminal window. When signaling a critical success, a major warning, or the start of a new configuration phase, drawing a visual "Box" or "Frame" around your text makes it immediately stand out. This adds a professional level of UI polish to your scripts without needing any external graphical libraries.

In this guide, we will demonstrate how to draw borders using standard ASCII characters and modern ANSI escape codes.

Method 1: The Simple ASCII Box

This method works on all versions of Windows and uses basic characters like +, -, and |.

The Implementation Script

To make the box dynamic (adjusting to the length of the string), we calculate the string length and build the border to match.

@echo off
setlocal enabledelayedexpansion

set "msg=SUCCESS: SYSTEM INITIALIZED"

:: Calculate the length of the message string using binary search
:: A sentinel character is appended so the final character is always counted
set "s=!msg!x"
set "len=0"
for %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if not "!s:~%%n,1!"=="" (
set /a "len+=%%n"
set "s=!s:~%%n!"
)
)

:: Build border: message length + 2 for one space padding on each side
set /a "borderLen=len + 2"
set "border="
for /L %%i in (1,1,!borderLen!) do set "border=!border!-"

echo +!border!+
echo ^| !msg! ^|
echo +!border!+

endlocal
pause

Method 2: The "Double Line" Box (Extended ASCII)

Windows terminals support extended ASCII characters from code page 437. These give you professional-looking double-line borders.

  • : Alt+201 (top-left corner)
  • : Alt+187 (top-right corner)
  • : Alt+200 (bottom-left corner)
  • : Alt+188 (bottom-right corner)
  • : Alt+205 (horizontal line)
  • : Alt+186 (vertical line)

Implementation Script

@echo off
:: Set code page to UTF-8
chcp 65001 >nul

:: Optional: hide cursor flicker
cls

:: Header Frame using Unicode box-drawing
echo ╔════════════════════════════════════╗
echo ║ ADMINISTRATIVE TOOL ║
echo ╚════════════════════════════════════╝

echo.
echo Process starting...
pause
Note on character encoding

The double-line box characters (, , , etc.) are from the OEM character set (code page 437) used by cmd.exe by default. If your batch file is saved in UTF-8 or another encoding, these characters may not display correctly. Most Windows text editors preserve the correct encoding when saving .bat files, but if you see garbled output, check your editor's encoding settings.

Method 3: Dynamic ANSI Frames (Windows 10/11)

If you are on a modern system, you can use ANSI escape codes to draw a colored box frame with contrasting text inside.

@echo off
setlocal DisableDelayedExpansion

:: 1. Capture ESC exactly like your working highlighter
for /F %%A in ('echo prompt $E ^| cmd') do set "ESC=%%A"

if not defined ESC (
echo [ERROR] Could not generate ANSI escape character.
pause
exit /b 1
)

:: Now it's safe to turn delayed expansion on
setlocal EnableDelayedExpansion

:: 2. Define colors once
set "G=!ESC![92m"
set "W=!ESC![97m"
set "R=!ESC![0m"

cls

:: 3. Draw box - every line starts with color and ends with reset
echo !G!+------------------------------------+!R!
echo !G!^| ^|!R!
echo !G!^| !W!SECURE SESSION ACTIVE!G! ^|!R!
echo !G!^| ^|!R!
echo !G!+------------------------------------+!R!

endlocal
endlocal
pause

Automating the Frame (Reusable Function)

Instead of manually drawing every line, you can create a :DrawBox label in your script that takes a message and automatically wraps it in a correctly sized frame.

@echo off
setlocal enabledelayedexpansion

call :DrawBox "UPDATING REGISTRY HIVE"
call :DrawBox "RESTARTING SERVICES"
call :DrawBox "ALL TASKS COMPLETE"

endlocal
pause
exit /b

:DrawBox
setlocal enabledelayedexpansion
set "str=%~1"

:: Calculate string length using binary search with sentinel
set "s=!str!x"
set "len=0"
for %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if not "!s:~%%n,1!"=="" (
set /a "len+=%%n"
set "s=!s:~%%n!"
)
)

:: Build border (message length + 2 for one space on each side)
set /a "borderLen=len + 2"
set "border="
for /L %%i in (1,1,!borderLen!) do set "border=!border!-"

echo +!border!+
echo ^| !str! ^|
echo +!border!+
echo.

endlocal
exit /b

Best Practices

  1. Escaping the Pipe Character: In Batch, the | character is a reserved symbol for piping data between commands. If you use it as a visual border in an echo command, you MUST escape it with a caret (^|). The double-line character (Alt+186) is a different character entirely and does not require escaping.
  2. Width Management: Standard CMD windows are 80 characters wide. Ensure your boxes aren't too wide, or they will wrap and look like a jumbled mess.
  3. Contrast: If you use boxes for error messages, consider using Method 3 to make the border Red (!ESC![91m).

Conclusion

Drawing a frame around your text output is a small detail that makes a massive impact on the usability of your scripts. It separates headers from data, highlights critical notices, and gives your command-line tools a distinct "identity." Whether you use standard ASCII, double-line extended characters, or modern ANSI colors, framing your output orients your users and provides a clear, professional visual hierarchy.