batch-file - 批量编写实时进度条

标签 batch-file

我想使用实时进度条,它会在编写代码或安装某些东西或加载文件时更新。

例子:

@echo off

:main
echo Updating file...
[PROGRESS BAR HERE]

“PROGRESS BAR HERE”表示我想要放置实时进度条的部分。

最佳答案

只是一个骷髅。根据需要进行调整。

基本思想是输出带有结束回车的进度条的行以返回到行的开头,并能够在前一个状态上重新绘制下一个状态。

所有“有问题”的代码都包含在子程序中,因此您只需要 call :drawProgressBar percentValue "operationText"

@echo off

    setlocal enableextensions disabledelayedexpansion

    for /l %%f in (0 1 100) do (
        call :drawProgressBar %%f "up test with a long text that will not fit on screen unless you have a lot of space"
    )
    for /l %%f in (100 -1 0) do (
        call :drawProgressBar %%f "going down test"
    )
    for /l %%f in (0 5 100) do (
        call :drawProgressBar !random! "random test"
    )

    rem Clean all after use
    call :finalizeProgressBar 1


    call :initProgressBar "|" " "
    call :drawProgressBar 0 "this is a custom progress bar"
    for /l %%f in (0 1 100) do (
        call :drawProgressBar %%f 
    )

    endlocal
    exit /b


:drawProgressBar value [text]
    if "%~1"=="" goto :eof
    if not defined pb.barArea call :initProgressBar
    setlocal enableextensions enabledelayedexpansion
    set /a "pb.value=%~1 %% 101", "pb.filled=pb.value*pb.barArea/100", "pb.dotted=pb.barArea-pb.filled", "pb.pct=1000+pb.value"
    set "pb.pct=%pb.pct:~-3%"
    if "%~2"=="" ( set "pb.text=" ) else ( 
        set "pb.text=%~2%pb.back%" 
        set "pb.text=!pb.text:~0,%pb.textArea%!"
    )
    <nul set /p "pb.prompt=[!pb.fill:~0,%pb.filled%!!pb.dots:~0,%pb.dotted%!][ %pb.pct% ] %pb.text%!pb.cr!"
    endlocal
    goto :eof

:initProgressBar [fillChar] [dotChar]
    if defined pb.cr call :finalizeProgressBar
    for /f %%a in ('copy "%~f0" nul /z') do set "pb.cr=%%a"
    if "%~1"=="" ( set "pb.fillChar=#" ) else ( set "pb.fillChar=%~1" )
    if "%~2"=="" ( set "pb.dotChar=." ) else ( set "pb.dotChar=%~2" )
    set "pb.console.columns="
    for /f "tokens=2 skip=4" %%f in ('mode con') do if not defined pb.console.columns set "pb.console.columns=%%f"
    set /a "pb.barArea=pb.console.columns/2-2", "pb.textArea=pb.barArea-9"
    set "pb.fill="
    setlocal enableextensions enabledelayedexpansion
    for /l %%p in (1 1 %pb.barArea%) do set "pb.fill=!pb.fill!%pb.fillChar%"
    set "pb.fill=!pb.fill:~0,%pb.barArea%!"
    set "pb.dots=!pb.fill:%pb.fillChar%=%pb.dotChar%!"
    set "pb.back=!pb.fill:~0,%pb.textArea%!
    set "pb.back=!pb.back:%pb.fillChar%= !"
    endlocal & set "pb.fill=%pb.fill%" & set "pb.dots=%pb.dots%" & set "pb.back=%pb.back%"
    goto :eof

:finalizeProgressBar [erase]
    if defined pb.cr (
        if not "%~1"=="" (
            setlocal enabledelayedexpansion
            set "pb.back="
            for /l %%p in (1 1 %pb.console.columns%) do set "pb.back=!pb.back! "
            <nul set /p "pb.prompt=!pb.cr!!pb.back:~1!!pb.cr!"
            endlocal
        )
    )
    for /f "tokens=1 delims==" %%v in ('set pb.') do set "%%v="
    goto :eof

关于batch-file - 批量编写实时进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108380/

相关文章:

batch-file - 如何在批处理文件中重复%random%命令?

windows - 如何仅使用 Windows 的内置功能从脚本中压缩或解压缩?

windows - 在批处理文件中使用时 TaskList 命令挂起

java - 登录远程 Windows 服务器并执行 .bat 文件

winapi - 批处理 (.bat) 文件的新窗口问题

windows - 如何禁止在批处理脚本中键入消息?

windows - 差异 REG QUERY 本地与远程计算机批处理脚本

powershell - 具有延迟变量扩展的 For 循环中 "!"文字问题的解决方法

java - 是否可以以 "batch"方式在 Java 中执行 Windows 命令?即一个接一个但坚持不懈?

command-line - 批处理文件中的字符串替换