【问题标题】:windows cmd for loop cycle and time counting + percentagewindows cmd for循环周期和时间计数+百分比
【发布时间】:2018-05-15 23:00:54
【问题描述】:

首先我正在寻找一个代码,如果这里或其他地方已经存在, 但没有找到符合我要求的结果。 所以我开始逐步建立它。 我设置了一些东西,比如获取总数, 如果不存在则创建临时文件。 如果有记录的文件存在而不是设置记录的完成量 ,在将其放入循环“for”之后 并计算所有内容以显示结果也与百分比。

所以这实际上是我所做的结果。

@echo off
setlocal enableExtensions enableDelayedExpansion
set /p NUMBER=<total_number.info
set INFO_FILE=total_done.info
IF NOT EXIST %INFO_FILE% (
    (echo 1) > %INFO_FILE%
)
set /p NUMBER_DONE=<%INFO_FILE%
for /l %%x in (%NUMBER_DONE%, 1, %NUMBER%) do (
    set /a "Total_Left=%NUMBER%-%%x"
    set /a "percent=(%%x*100)/%NUMBER%"
    if !Total_Left! GEQ 1 (
        set /a "Seconds=!Total_Left!*2"
    ) else (
        set /a "Seconds=00"
    )
    if !Seconds! GEQ 60 (
        set /a "Minutes=!Seconds!/60"
    ) else (
        set /a "Minutes=00"
    )
    if !Minutes! GEQ 60 (
        set /a "Hours=!Minutes!/60"
    ) else (
        set /a "Hours=00"
    )
    if !Hours! GEQ 24 (
        set /a "Days=!Hours!/24"
    ) else (
        set /a "Days=0"
    )
    set /a "T_L_H=!Hours!-(!Days!*24)"
    set /a "T_L_M=!Minutes!-(!Hours!*60)"
    set /a "T_L_S=!Seconds!-(!Minutes!*60)"
    set "T_L_Ho=0!T_L_H!"
    set "T_L_Mi=0!T_L_M!"
    set "T_L_Se=0!T_L_S!"
    TITLE Test - Percent done : !percent!%%
    echo.
    echo Time Left : !Days! Days !T_L_Ho:~-2!:!T_L_Mi:~-2!:!T_L_Se:~-2!
    echo.
    echo Total pages to be done : %NUMBER%
    echo Pages done : %%x
    echo Rest of the pages : !Total_Left!
    echo Percent done : !percent!%%
    if %%x GEQ %NUMBER_DONE% (
        (echo %%x) > %INFO_FILE%
        timeout 1 > NUL
    )
    cls
)
pause>NUL

脚本上次更新时间 12.1.2018 23:45

【问题讨论】:

    标签: for-loop time cmd percentage counting


    【解决方案1】:

    这是一个 PowerShell 脚本,它将使用包含当前步骤编号的文件来监控进度。请务必在启动前创建监视文件。

    <#
    .SYNOPSIS
    This cmdlet displays a progress bar based on monitoring a watch file.
    
    .DESCRIPTION
    If no PROMPT string is provided, the $Env:PROMPT string will be used.
    
    .PARAMETER TotalCount
    Specify the total number of steps to monitor.
    
    .PARAMETER WatchFile
    Specify the path to the file containing the number of steps completed.
    
    .EXAMPLE
    Monitor-Progress -TotalCount 10 -WatchFile wf.txt
    #>
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true)]
        [int]$TotalCount
    
        ,[Parameter(Mandatory=$true)]
        [ValidateScript({Test-Path $_ -PathType 'Leaf'})]
        [string]$WatchFile
    )
    
    $currentcount = 0
    $previouscount = $currentcount
    
    while ($currentcount -lt $TotalCount) {
        $currentcount = [int32](Get-Content $WatchFile)
    
        if ($currentcount -ne $previouscount) {
            Write-Progress -Activity "Watching long progress" `
                -percentComplete ($currentcount / $TotalCount*100)
            $previouscount = $currentcount
        }
    }
    

    如果您需要从 cmd.exe shell 运行它,您可以:

    powershell -NoProfile -File Monitor-Progress.ps1 -TotalCount 5 -WatchFile wf.txt
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多