How to Create a Header and Footer in Console Output in Batch Script
Visual structure is key to making a command-line tool user-friendly. Without clear boundaries, the start and end of a script's output can get lost in the noise of previous commands or system messages. A Header establishes context (what is this script?), while a Footer signals finality (did it finish successfully?).
In this guide, we will demonstrate how to create professional headers and footers using ASCII borders, dynamic timestamps, and color formatting.
Designing a Standard Header
A good header should contain the tool's name, version, and the current date/time to provide an audit trail in logs.
@echo off
setlocal enabledelayedexpansion
:: Setup for colors
for /F "delims=" %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
if not defined ESC (
echo [WARNING] Could not generate ANSI escape character. Colors disabled.
set "C_BLUE="
set "C_WHITE="
set "C_GRAY="
set "C_GREEN="
set "C_RESET="
) else (
set "C_BLUE=!ESC![94m"
set "C_WHITE=!ESC![97m"
set "C_GRAY=!ESC![90m"
set "C_GREEN=!ESC![92m"
set "C_RESET=!ESC![0m"
)
call :DrawHeader
:: Rest of the script logic goes here...
echo [INFO] Pinging Gateway...
ping -n 2 127.0.0.1 >nul
:: Call the Footer at the end
call :DrawFooter
endlocal
pause
exit /b
:DrawHeader
cls
echo !C_BLUE!======================================================!C_RESET!
echo !C_WHITE! NETWORK DIAGNOSTIC TOOL v2.1!C_RESET!
echo !C_GRAY! Started: !DATE! at !TIME!!C_RESET!
echo !C_BLUE!======================================================!C_RESET!
echo.
exit /b
:DrawFooter
echo.
echo !C_BLUE!------------------------------------------------------!C_RESET!
echo !C_GREEN! [OK] Operation Completed Successfully.!C_RESET!
echo !C_BLUE!------------------------------------------------------!C_RESET!
exit /b
If you're on an older Windows version where cmd.exe simply doesn't support ANSI colors you would not see colors! You would need Windows Terminal or PowerShell instead.
Creating a Reusable Section Header Function
If you have multiple sections in your script, you can create a reusable function that wraps any text in a header-style format with a dynamically sized underline.
@echo off
setlocal enabledelayedexpansion
call :SectionHeader "SYSTEM AUDIT"
echo Checking user accounts...
echo Checking group policies...
echo.
call :SectionHeader "DISK CLEANUP"
echo Scanning temporary files...
echo Removing orphaned entries...
echo.
endlocal
pause
exit /b
:SectionHeader
setlocal enabledelayedexpansion
set "title=%~1"
:: Calculate title length using binary search with sentinel
set "s=!title!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 underline matching the bracket-wrapped title width
:: "[ TITLE ]" adds 4 characters (bracket, space, space, bracket)
set /a "lineLen=len + 4"
set "line="
for /L %%i in (1,1,!lineLen!) do set "line=!line!-"
echo.
echo [ !title! ]
echo !line!
echo.
endlocal
exit /b
Using Footers for Error Handling
A footer isn't just for showing "Success." It is often the best place to report the summary of an operation, including how many errors occurred.
@echo off
setlocal enabledelayedexpansion
set "errors=0"
set "total=0"
:: Simulated logic that processes items and tracks errors
for %%F in (*.txt) do (
set /a "total+=1"
:: Simulate occasional failures
set /a "check=total %% 3"
if !check! equ 0 set /a "errors+=1"
)
echo.
echo ======================================================
if !errors! equ 0 (
echo SUMMARY: All !total! tasks passed without error.
) else (
echo SUMMARY: Finished !total! tasks with !errors! issues found.
)
echo ======================================================
endlocal
pause
Comparisons: Simple vs. Multi-line Headers
- Simple Header (
echo --- NAME ---): Best for quick utility scripts used by developers. - Boxed Header (Method 1): Best for customer-facing tools, installer scripts, or monitoring dashboards that stay open on a screen for long periods.
- Dynamic Header: Best for automated logs where you need to know exactly when "Phase 1" started relative to "Phase 2."
Best Practices
- Keep it Consistent: Use the same character (e.g.,
=,-, or*) for both the top and bottom of your frames. - Use Colors Sparingly: Use Blue or Cyan for headers, and Green/Red for the status messages inside the footer. Avoid "Rainbow" headers which can be distracting.
- Clear the Screen: Unless your script is designed to append to a previous list, start your Header with a
clscommand to give the user a fresh "application-like" experience. - Include Context: In administrative contexts, it is helpful to include the current working directory (
!CD!) or the machine name (!COMPUTERNAME!) in the header for audit purposes.
Conclusion
Headers and footers are the "packaging" of your Batch script. They tell the user that they are in the right place and that the process has reached its logical conclusion. By implementing consistent, well-formatted boundaries around your output, you elevate your code from a series of commands to a professional, communicative tool that is both functional and easy to navigate.