Windows 计算成批运行的脚本/命令的运行时间

Windows 计算成批运行的脚本/命令的运行时间,windows,batch-file,cmd,windows-10,Windows,Batch File,Cmd,Windows 10,我试图计算/插入命令/脚本执行所需的时间。这是批处理文件: @echo关闭 setlocal EnableDelayedExpansion echo“正在启动脚本运行…”echo/ 设置“开始时间=%time:=0%” echo“初始启动:%startTime%”和echo/ (调用node test_node_script.js)和&( 设置“endTime=%time:=0%” 回显“初始结束:%endTime%” rem获取经过的时间: 设置“结束=!结束时间:%time:~8,1%=%1

我试图计算/插入命令/脚本执行所需的时间。这是批处理文件:

@echo关闭
setlocal EnableDelayedExpansion
echo“正在启动脚本运行…”echo/
设置“开始时间=%time:=0%”
echo“初始启动:%startTime%”和echo/
(调用node test_node_script.js)和&(
设置“endTime=%time:=0%”
回显“初始结束:%endTime%”
rem获取经过的时间:
设置“结束=!结束时间:%time:~8,1%=%100)*100+1!”&设置“开始=!开始时间:%time:~8,1%=%100)*100+1!”
设置/A“elap=((10!结束时间:~2,1%=%100)*60+1!%%100)-((10!开始时间:~2,1%=%100)*60+1!%%100),elap-=(elap>>31)*24*60*60*100”
设置/A“总时间=elap/100”
echo“Elaps时间:%totalTime%”和echo/
rem将经过的时间转换为HH:MM:SS:CC格式:
设置/A“cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100”
echo“开始:%startTime%”和echo/
回显“结束:%endTime%”和回显/
echo“已用时间:%hh:~1%%时间:~2,1%%mm:~1%%时间:~2,1%%ss:~1%%时间:~8,1%%cc:~1%”和echo/
) || (
echo“脚本失败”&echo/
)
示例节点脚本(
test\u node\u script.js
):

异步函数init(){ 控制台日志(1); 等待睡眠(1000); 控制台日志(2); } 功能睡眠(ms){ 返回新承诺((解决)=>{ 设置超时(解析,毫秒); }); } init() 问题是由于某种原因,批处理文件中没有设置
endTime

"Starting the script run...."

"Initial Start:    16:45:28.56"

1
2
"Initial End:      "
"Elaps time: 0"

"Start:    16:45:28.56"

"End:      "

"Elapsed:  00:00:00.00"
如何获取
结束时间
,使此计算有效?

来自我的库:

@rem Taken from https://stackoverflow.com/a/6209392/3150445 and modified to allow composability
@rem and to eliminate the ~1..2cs overhead of loading another instance of cmd.exe.

@if "%1" equ "-?" goto :ShowHelpAndExit
@if "%1" equ "/?" goto :ShowHelpAndExit

@rem Measure the time to run whatever is on the command line.
@set _start_=%time%
@call %*
@set _CmdErrorLevel_=%ERRORLEVEL%
@set _end_=%time%

@call :OutputElapsedTime

@rem Clean-up and return the result from whatever we just timed for the caller.
@rem We could not put everything within a setlocal block and preserve the
@rem effects caused by whatever is in %*.
@set _start_=
@set _end_=
@set _CmdErrorLevel_=&exit /b %_CmdErrorLevel_%&

:OutputElapsedTime
@setlocal

@rem Break _start_ and _end_ into seperate hour, minute, second and centisecond values.
@rem _options_ works in locales that use period or comma for decimal fractions.
@set _options_="tokens=1-4 delims=:.,"
@for /f %_options_% %%a in ("%_start_%") do @call :SplitTime _start %%a %%b %%c %%d
@for /f %_options_% %%a in ("%_end_%") do @call :SplitTime _end %%a %%b %%c %%d

@rem Calculate the run-time.
@set /a _hours_=%_end_h_%-%_start_h_%
@set /a _mins_=%_end_m_%-%_start_m_%
@set /a _secs_=%_end_s_%-%_start_s_%
@set /a _cs_=%_end_cs_%-%_start_cs_%

@rem Correct for under-flows.
@if %_cs_% lss 0 @set /a _secs_ = %_secs_% - 1 &@set /a _cs_ = 100%_cs_%
@if %_secs_% lss 0 @set /a _mins_ = %_mins_% - 1 &@set /a _secs_ = 60%_secs_%
@if %_mins_% lss 0 @set /a _hours_ = %_hours_% - 1 &@set /a _mins_ = 60%_mins_%
@if %_hours_% lss 0 @set /a _hours_ = 24%_hours_%

@rem Zero padd the centiseconds if needed.
@if 1%_cs_% lss 100 @set _cs_=0%_cs_%

@rem Display summary results.
@set /a _totalsecs = %_hours_%*3600 + %_mins_%*60 + %_secs_%
@echo.
@echo TimeIt Result: %_hours_%:%_mins_%:%_secs_%.%_cs_% (%_totalsecs%.%_cs_%s total)
@if %_totalsecs% equ 0 (
  @if %_cs_% equ 0 (
    @echo An all zero result indicates command took less that 1/100 of a second.
  )
)
@exit /b 0

:ShowHelpAndExit
@set _HelpFile=%~dpn0.cmd.help.txt
@if exist %_HelpFile% (@type %_HelpFile%) else (@echo Help file %_HelpFile% not found. &_HelpFile=&exit /b %_ERROR_FILE_NOT_FOUND_%)
@exit /b %_ERROR_SUCCESS_%

@rem Takes a prefix string, hours, minutes, seconds and centiseconds parameters
@rem and sets prefix_h, prefix_m, prefix_s and prefix_cs_ environment variables.
:SplitTime
@set %1_h_=%2
@set /a %1_m_=100%3 %% 100
@set /a %1_s_=100%4 %% 100
@set /a %1_cs_=100%5 %% 100
@exit /b 0
将上述内容另存为
timeit.cmd
,然后将以下自述文件另存为
timeit.cmd.help.txt

TimeIt.cmd Help

Description:
Runs whatever is passed on the command line, times the execution and prints
the elapsed time. Can be run from the command line or within a script.  All
side effects (environment variables) of the command(s) are preserved and the
status value returned.

Arguments:
/?
-? Spew this help content.

Everything else is passed to a `call %*` as-is and the ERRORVALUE
returned to caller.

Usage: TimeIt.cmd <whatever needs to be timed>
TimeIt.cmd帮助
说明:
运行在命令行上传递的任何内容,计算执行时间并打印
经过的时间。可以从命令行或在脚本中运行。全部
命令的副作用(环境变量)将保留,并且
返回的状态值。
论据:
/?
-?吐出此帮助内容。
其他所有内容都按原样传递给“call%*`和ERRORVALUE
返回给来电者。
用法:TimeIt.cmd

这回答了你的问题吗?看起来你启用了延迟扩展,但没有使用它。如果你想在同一个代码块中定义和使用变量,在这种情况下,括号中的所有内容都需要用
不是
%
。因此
!endTime!
是其中一个,b但还有更多。