我想制作一个批处理脚本,以计算它运行了多长时间(以秒为单位)。我该怎么办?
答案 0 :(得分:0)
希望您会喜欢。 > u << / p>
将此添加到要计算其运行时间的批处理的第一行:
echo %time% >"starting time.any-file-type"
然后将其添加到批处理文件的末尾:
(start) "counter.bat"
“ counter.bat”的代码
@echo off
set thetimenow=%time%
set /p thetimebefore=<"starting time.any-file-type"
set /a hours=%thetimenow:~0,2% - %thetimebefore:~0,2%
set /a mins=%thetimenow:~3,2% - %thetimebefore:~3,2%
set /a seconds=%thetimenow:~6,2% - %thetimebefore:~6,2%
set /a minsecs=%thetimenow:~9,2% - %thetimebefore:~9,2%
if %minsecs% lss 0 (set /a seconds-=1
set /a minsecs+=100)
if %seconds% lss 0 (set /a mins-=1
set /a seconds+=60)
if %mins% lss 0 (set /a hours-=1
set /a mins+=60)
if %hours% lss 0 (set /a days=1
set /a hours+=24)
if not "%days%" == "1" (set days=) else (set days=1 or more days, )
echo The batch required has ran for %days%%hours% hour(s), %mins% minute(s), %seconds% second(s), and %minsecs% miniseconds.
那是我能提供的最好的东西。
请注意,所需的批处理中不得包含关键字exit
。
如果需要,请在包含chcp 437
的行之前使用%time%
。
如果您不想修改需要计数器的代码,请确保批次位于同一文件夹中。
答案 1 :(得分:0)
您可以在批处理脚本中尝试以下示例:
@echo off
Title Get Duration Time of Running Batch Script
setlocal enabledelayedexpansion
mode 90,20 & color 0B
set STARTTIME=!TIME!
REM Your Main Program goes here
Ping www.google.com
set ENDTIME=!TIME!
echo(
call :GetDuration !STARTTIME! !ENDTIME!
pause>nul
REM -----------------------------------------------------------------------------------------------------------
:GetDuration
set function_starttime=%1
set function_endtime=%2
rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_starttime%") do (
set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start
rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
set DURATION=%hh%:%mm%:%ss%.%cc%
echo Start Time : %function_starttime%
echo Finish Time : %function_endtime%
echo ---------------
echo Duration : %DURATION%
echo(
Exit /b
REM -----------------------------------------------------------------------------------------------------------