0

I have problem with convert %time% in for loop into seconds.

This is my script.

@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!
    )

My intension is to get time execution of StartSomeApp.exe in seconds, what is the simplest way to convert StartDate and EndDate into seconds ?

Konrad
  • 656
  • 4
  • 15
  • 32
  • I think this answer could be useful: http://stackoverflow.com/questions/4487100/how-can-i-use-a-windows-batch-file-to-measure-the-performance-of-console-applica – kragan Apr 08 '15 at 14:33

3 Answers3

1

This is a duplicate question, it has already been covered by SO here.

Basically after setting %STARTTIME% = %TIME% and endtime as you've done, you need to format the timing or if you want it only in seconds, you can just convert it to centiseconds, like so (code taken from the top answer in that question)

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%

There's also other variations that use for loops for example.

Community
  • 1
  • 1
matrixanomaly
  • 5,865
  • 2
  • 32
  • 50
  • I have a little problem , when inserting this code into for loop. – Konrad Apr 08 '15 at 16:01
  • @Konrad what is the issue? Although it's noted by others that this has 2 special cases that it doesn't handle, again see the link I gave in my post for other variations, see if they work for you. – matrixanomaly Apr 08 '15 at 16:17
  • Thanks for your reply, I'v tried to add your advice to my script I have problem with insert Your code into for loop. In this link is the code I corrected , http://pastebin.com/XSfn1a2a I seems to by wrong. – Konrad Apr 08 '15 at 16:32
  • @Konrad the parenthesis inside the set commands are seen as closing parenthesis, hence the errror "unbalanced parenthesis". see this: http://stackoverflow.com/questions/6478606/batch-file-if-statement-fails-with-was-unexpected-at-this-time try googling what other errors you might have. – matrixanomaly Apr 08 '15 at 17:13
  • @ matrixanomaly Thanks to Your adviced I wrote appropriate script. – Konrad Apr 08 '15 at 20:23
1

You could also convert to unix timestamp and calculate the seconds that way. Here's an example:

@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
BinaryPatrick
  • 492
  • 6
  • 15
0

I corrected my script , now it is done. i looks like this.

@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!

    )
Konrad
  • 656
  • 4
  • 15
  • 32