[英]Batch file progress percentage
How can I show the progress of a long running operation in windows batch (cmd) file in percentage?如何以百分比显示 Windows 批处理 (cmd) 文件中长时间运行的操作的进度? Can you share some example code?
你能分享一些示例代码吗?
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
An example would be scanning a large file/database, showing progress instead of a blinking cursor, for ages!一个例子是扫描一个大文件/数据库,显示进度而不是闪烁的光标,多年来!
set c=<no. of lines in file>
for /l %i in (1,1,%c%) do cls & set /p="Scanning for target ... "<nul & (set /a per=%i00/%c% >nul & echo !per!%)& ping 127.0.0.1 -n 2 > nul & REM when target found, send agents to greet
echo Complete.
The code in brackets ()
are for progress percentage.括号
()
中的代码表示进度百分比。
Tested in Win 10 CmD>在 Win 10 CmD 中测试>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.