How to Display a Confirmation Summary Before Executing Actions in Batch Script
One of the most dangerous things an automation script can do is perform destructive actions (like deleting files or wiping a registry key) without first confirming exactly what it "thinks" it is about to do. A Confirmation Summary provides a final overview of the variables and targets, giving the user one last chance to spot an error before the "Point of No Return."
In this guide, we will demonstrate how to build a formatted summary screen and use the choice command for the final execution trigger.
The Strategy: The "Audit" Phase
Before reaching the execution block of your script, you should:
- Gather all user inputs and environment variables.
- Format them into a clean, boxed summary.
- Pause and require a positive "Yes" to proceed.
Implementation Script: File Deployment Summary
@echo off
setlocal enabledelayedexpansion
:: 1. Simulation of variables set earlier in the script
set "source=C:\Projects\Marketing\Assets"
set "destination=\\FILESERVER\Public\2024"
set "fileCount=142"
set "action=OVERWRITE"
set "adminOnly=TRUE"
:: 2. Display the Summary Box
cls
echo ======================================================
echo EXECUTION PREVIEW (SUMMARY^)
echo ======================================================
echo.
echo SOURCE PATH: !source!
echo DESTINATION: !destination!
echo.
echo TOTAL FILES: !fileCount!
echo MODE: !action!
echo RESTRICTIONS: !adminOnly!
echo.
echo ======================================================
echo.
:: 3. The Final Confirmation
echo WARNING: The selected action is DESTRUCTIVE.
choice /c YN /m "Do you want to proceed with the execution?"
if !errorlevel! equ 2 (
echo.
echo [ABORT] Execution canceled by user.
pause
exit /b 1
)
:: 4. The actual action block
echo.
echo [RUNNING] Processing data...
:: (Actual commands go here)
pause
exit /b 0
Since setlocal enabledelayedexpansion is active, use !variable! syntax instead of %variable% inside code blocks where variables may be set or changed at runtime. This ensures consistent expansion behavior.
Enhancing the Summary with ANSI Colors
On modern Windows 10 and later, using colors in your summary makes it much easier to read. Use yellow for paths and red for the mode.
@echo off
setlocal enabledelayedexpansion
:: 1. Simulation of variables set earlier in the script
set "source=C:\Projects\Marketing\Assets"
set "destination=\\FILESERVER\Public\2024"
set "fileCount=142"
set "action=OVERWRITE"
set "adminOnly=TRUE"
:: 2. Generate ESC character for ANSI codes
for /f %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set "YL=!ESC![93m"
set "RD=!ESC![91m"
set "GN=!ESC![92m"
set "RS=!ESC![0m"
:: 3. Display the Summary Box with Colors
cls
echo ======================================================
echo EXECUTION PREVIEW (SUMMARY^)
echo ======================================================
echo.
echo SOURCE PATH: !YL!!source!!RS!
echo DESTINATION: !YL!!destination!!RS!
echo.
echo TOTAL FILES: !GN!!fileCount!!RS!
echo MODE: !RD!!action!!RS!
echo RESTRICTIONS: !adminOnly!
echo.
echo ======================================================
echo.
:: 4. The Final Confirmation
echo !RD!WARNING: The selected action is DESTRUCTIVE.!RS!
choice /c YN /m "Do you want to proceed with the execution?"
if !errorlevel! equ 2 (
echo.
echo [ABORT] Execution canceled by user.
pause
exit /b 1
)
:: 5. The actual action block
echo.
echo !GN![RUNNING]!RS! Processing data...
:: (Actual commands go here)
pause
exit /b 0
ANSI escape sequences require Windows 10 version 1511 or later. On older systems, the raw escape codes will appear as garbled text. Test your target environment before relying on colored output.
Why Every Script Needs a Summary
- Path Verification: It is easy to accidentally type
C:\Userinstead ofC:\Users. A summary allows the user to see the full path clearly before adel /qcommand runs. - Environment Audit: If a script is sensitive to whether it is running on a "Production" or "Development" server, displaying the
%COMPUTERNAME%in the summary is essential. - Variable Visibility: Dynamic variables (like those generated by a
forloop) can sometimes be empty if a previous step failed. The summary shows these empty values clearly.
Best Practices
- Use Boxes/Lines: Visual separators (like
===or***) help the user focus on the data rather than the surrounding console text. - Explicit Abort: If the user chooses "No," always explicitly state
[ABORT]so they aren't left wondering if the script finished anyway. - Summary to File: For mission-critical tasks, append the same summary to a
log.txtfile before executing, so you have a record of what parameters were used. - Formatting: Align the labels (e.g.,
SOURCE:,TARGET:) using spaces so they form a clean vertical column. - Exit Codes: Return distinct exit codes (
exit /b 0for success,exit /b 1for abort) so calling scripts or schedulers can detect the outcome.
For mission-critical deployments, consider writing the summary block to a log file using >>log.txt redirection before the confirmation prompt. This creates an audit trail regardless of whether the user proceeds or aborts.
Conclusion
A confirmation summary is the difference between a "reckless" script and a professional administrative tool. It adds a layer of human-in-the-loop safety that prevents catastrophic mistakes and builds user confidence. By taking the time to present your script's "plan" before execution, you ensure that your automation is both powerful and responsible.