Batch file progress percentage

2020-07-27 05:38发布

问题:

How can I show the progress of a long running operation in windows batch (cmd) file in percentage? Can you share some example code?

回答1:

Here is how...

Note: This code is a slightly modified version of this answer.

@echo off

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

FOR /L %%n in (1,1,10) DO (
    call :show_progress %%n 10
    ping localhost -n 2 > nul
)

echo Done!
exit /b

:show_progress
setlocal EnableDelayedExpansion
set current_step=%1
set total_steps=%2
set /a "progress=(current_step * 100) / total_steps"

set /p ".=Progress: !progress!%%!CR!" <nul

if !progress! equ 100 echo.

exit /b