windows cmd for循环周期和时间计数+百分比

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

首先,我正在寻找代码,如果已经存在于此处或其他地方,但发现我的要求没有结果。所以我开始逐个构建它。我设置了一些东西,如获取总数,而不是创建临时文件,如果不存在。如果存在记录的文件比设置了多少记录,则将其放入循环周期“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个回答
0
投票

这是一个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
© www.soinside.com 2019 - 2024. All rights reserved.