0

I found this nice little tidbit of code here: https://stackoverflow.com/a/5262637/2128987

@echo off

set starttime=%TIME%
set startcsec=%STARTTIME:~9,2%
set startsecs=%STARTTIME:~6,2%
set startmins=%STARTTIME:~3,2%
set starthour=%STARTTIME:~0,2%
set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)

:TimeThis
robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np folder%rndfolder% %drvltr%:\f%dirnew%\

set endtime=%time%
set endcsec=%endTIME:~9,2%
set endsecs=%endTIME:~6,2%
set endmins=%endTIME:~3,2%
set endhour=%endTIME:~0,2%
if %endhour% LSS %starthour% set /a endhour+=24
set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)

set /a timetaken= ( %endtime% - %starttime% )
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%

echo.
echo Took: %timetaken% sec.

As a standalone program it works great. I am using it with a robocopy command basically to determine how long it takes to write a file.

I add one extra variable in it because I want to keep the raw seconds for calculation purposes. So I add the extra line set timeraw=%timetaken%:

set /a timetaken= ( %endtime% - %starttime% )
***set timeraw=%timetaken%***
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%

My batch file also uses setlocal enabledelayedexpansion

Well sometimes it does not properly calculate the "starttime" or "endtime". It's keeps it as the raw time in 08:30:22.35 type format and results in the error:

Invalid number. Numeric constants are either decimal (17),hexadecima (0x11), or octal (021)

Well obviously because it contains non-numeric characters like the : character.

My batch file goes in a continuous loop forever as I am using it to read, write, delete files and folders for a specific torture test condition.

Any idea why it would intermittently not calculate the starttime or endtime variables?

edit:

I made some changes to my overall script. I no longer need enabledelayedexpansion, cleaned up some if then statements, and simplified code a little. But I still occasionally get it where the starttime or endtime variables remain as the raw time format of HH:MM:SS.CS and causes error in calculation.

Community
  • 1
  • 1
HTWingNut
  • 13
  • 1
  • 1
  • 4

2 Answers2

0

Old question, but there are probably blocks of parentheses and when you change a variable within parentheses then you need to use delayed expansion.

Run this and examine the differences.

@echo off
set a=nothing
   if z==z (
      set a=b
      echo %a%
   )
pause
setlocal enabledelayedexpansion
set a=nothing
   if z==z (
      set a=b
      echo !a!
   )
pause
foxidrive
  • 37,659
  • 8
  • 47
  • 67
0

Gee - a question nearly a year old, with no answer.

I'll assume that the problem has now been solved, so as a matter of record, I'd conclude that the "sometimes it does not properly calculate" is because the hour/minute/second/hundredths will contain "08" or "09" which are not octal numbers.

The solution is

set /a startcsec=1%STARTTIME:~9,2% - 100

and repeat with each of the other 3 start time-segments; then repeat again with the end parts.

In addition, it could be that the hour is being presented with 0s suppressed. In this case, I'd suggest

set starttime=0%TIME: =%
set starttime=%startTIME:~-11%
set /a startcsec=1%STARTTIME:~9,2% - 100

where the first line prefixes the time with a '0', and replaces Space with [nothing]
the second selects just the last 11 characters of the result
and the last is the familiar form, using the resultant hh:mm:ss.cc format.

(obviously, the remainder of the substring-and-calculate method needs also to be implemented)

Magoo
  • 68,705
  • 7
  • 55
  • 76