我将for循环中的%time%转换为秒有问题。
这是我的剧本。
@Echo off
setlocal enabledelayedexpansion
set FilesDir=C:\
set FileName=lines.txt
IF EXIST !FileName! del /F !FileName!
FOR %%f IN (%FilesDir%*.*) DO (
Set StartTime=!time!
StartSomeApp.exe -f %%f
Set EndTime=!time!
set /A TimeOfExecutionInSeconds=!StartDate!-!EndDate!
Echo Started app with file %%f , TimeOfExecution: !TimeOfExecutionInSecond!
Echo Started app with file %%f , TimeOfExecution: !TimeOfExecutionInSecond!>>!FileName!
)
我的意图是在几秒钟内获得StartSomeApp.exe的执行时间,将StartDate和EndDate转换为秒的最简单方法是什么?
答案 0 :(得分:1)
这是一个重复的问题,SO here已经涵盖了它。
基本上在设置%STARTTIME%=%TIME%和结束时间之后,您需要格式化时间,或者如果您只想在几秒钟内完成,您可以将其转换为厘秒,就像这样(代码)取自该问题的最佳答案)
set STARTTIME = %TIME%
echo STARTTIME: %STARTTIME%
echo put your code here
set ENDTIME = %TIME%
echo ENDTIME: %ENDTIME%
rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)
set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)
rem calculating the duration
set /A DURATION=%ENDTIME%-%STARTTIME%
还有其他用于循环的变体。
答案 1 :(得分:0)
您还可以转换为unix时间戳并计算该方式的秒数。这是一个例子:
@echo off
call :GetUnixTime startTime
::DO STUFF
call :GetUnixTime endTime
set /a result = %startTime%-%endTime%
echo %result%
goto :EOF
:GetUnixTime
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
set "%1=%ut%" & goto :EOF
答案 2 :(得分:0)
我纠正了我的剧本,现在已经完成了。我看起来像这样。
@Echo off
setlocal enableextensions enabledelayedexpansion
set FilesDir=C:\
FOR /R %FilesDir% %%f IN (*.*) DO (
Set StartTime=!time!
:: timeout for about 6 seconds
timeout 6 >nul
Set EndTime=!time!
set /A ENDTIME=((((1!ENDTIME:~0,2!-100^)*360000^) +((1!ENDTIME:~3,2!-100^)*6000^)^) + (((1!ENDTIME:~6,2!-100^)*100^) +((1!ENDTIME:~9,2!-100^)^)^)^)
set /A STARTTIME=((((1!STARTTIME:~0,2!-100^)*360000^) +((1!STARTTIME:~3,2!-100^)*6000^)^) + (((1!STARTTIME:~6,2!-100^)*100^) +((1!STARTTIME:~9,2!-100^)^)^)^)
Echo !STARTTIME!
Echo !ENDTIME!
SET /A DurationInSeconds=((endtime-starttime^)/100^)
SET /A Duration=endtime-starttime
echo Duration in seconds: !DurationInSeconds!
echo Duration in centiseconds: !Duration!
)