批处理文件进度百分比

问题描述 投票:0回答:3

如何以百分比形式显示 Windows 批处理 (cmd) 文件中长时间运行的操作的进度?你能分享一些示例代码吗?

windows batch-file cmd
3个回答
0
投票

这是如何...

注意:此代码是this答案的稍微修改版本。

@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

0
投票

一个例子是扫描一个大文件/数据库,长时间显示进度而不是闪烁的光标!

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.

括号中的代码

()
是进度百分比。

在 Win 10 CmD 中测试>


0
投票
setlocal enabledelayedexpansion
@echo off
FOR /F "tokens=*" %%g IN ('find /v /c "" ^< %1') do (SET rows=%%g)
echo:
set /A target=%rows%/10
set /A percent=0
set /A line=0
for /F %%a in (%~n1%~x1) do ( 
  set /A line+=1
  SET /A mod = !line! %% !target!
  if !mod! == 0 (
                set /A percent+=10
                echo !percent!^%% at !TIME:~0,8!
                )
)
endlocal
© www.soinside.com 2019 - 2024. All rights reserved.