Text file formatting


I have the attached text file that I would like to reformat.
I am only interested to work with the time, the data after the time can be dumped.
The script must calculate the time between each entry.
The file must look like this after processing :

00:10:32 ( 00:04:13 )
00:14:45 ( 00:00:45 )
00:15:30 ( next time.... )

As you can see, I have added the time between the entries.
e.g : 00:04:13 represents the time that passed between 00:10:32 and 00:14:45

Thank you in advance !

00:10:32 - 342333
00:14:23 - 2343242394809848754533
00:15:34 - 908942859445345435345345345

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Look at this page for text manipulation


To calculate the difference, you'll need to extract the time in hour, minutes and seconds.  Convert to all seconds, do your math, then convert back to time format

I found an example here.  http://forums.techguy.org/software-development/757804-calculating-time-differences-batch-script.html

Bill PrewIT / Software Engineering ConsultantCommented:
Can the times ever wrap around midnight?

Does this have to be a BAT solution, or would VB script be okay (a little easier to do the calculations there)?

giberiskAuthor Commented:
Yes sir,. vb script would be perfect!
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

giberiskAuthor Commented:
Also C# .NET is ok, I can work with that too
giberiskAuthor Commented:
The time will never be 00:00:00 , if this is your concern
Bill PrewIT / Software Engineering ConsultantCommented:
Just a thought, would it be acceptable to do the following, rather than what you proposed?  I'm basically suggesting that you write on line n the difference in time from line n-1 to line n.  This would be a little cleaner to implement, since you could read line n and write it's value without having to read line n+1 first as you proposed.  So the result would be:

00:10:32 ( 00:00:00 )
00:14:45 ( 00:04:13 )
00:15:30 ( 00:00:45 )

giberiskAuthor Commented:
Of course, this would be great !
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this BAT solution a try, adjust the file paths at the top for input and output files.

@echo off
setlocal EnableDelayedExpansion

REM Define input and outfile locations, initialize prior time value
set InputFile=x:\ee\EE26936537\EE26936537.in
set OutputFile=x:\ee\EE26936537\EE26936537.out
set LastTime=

REM Read input records, calculate elapsed time from prior record and write out
  for /F "usebackq tokens=1" %%A in ("%InputFile%") do (
    REM First record has slightly different logic
    if defined LastTime (
      call :TimeDiff !LastTime! %%A 
      echo %%A ^( !TimeDiff! ^)
    ) else (
      echo %%A
    set LastTime=%%A
) > "%OutputFile%"
exit /b

:TimeDiff [start-time] [end-time]
  REM Convert each time from hh:mm:ss format to a number of seconds
  for /F "tokens=1-3 delims=:." %%A in ("%~1") do set /A "t1=(1%%C-100)+((1%%B-100)*60)+((1%%A-100)*3600)"
  for /F "tokens=1-3 delims=:." %%A in ("%~2") do set /A "t2=(1%%C-100)+((1%%B-100)*60)+((1%%A-100)*3600)"
  REM Adjust if end time crossed over midnight
  if %t1% GTR %t2% set /A "t2=t2+86400"
  REM Compute difference in seconds between times
  set /A "d=t2-t1"
  REM Compute hours, minuts and seconds from total seconds
  set /A "hh=d/3600" & set /A "mm=(d%%3600)/60" & set /A "ss=d%%60"
  REM Left pad with zero if needed for formatting
  if %hh% LSS 10 set "hh=0%hh%" & if %mm% LSS 10 set "mm=0%mm%" & if %ss% LSS 10 set "ss=0%ss%"
  REM Build returned result in hh:mm:ss format
  set "TimeDiff=%hh%:%mm%:%ss%"
  exit /b

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
giberiskAuthor Commented:

I have solved the problem in excel.
But i will accept your solution anyway, thank you for your time
Bill PrewIT / Software Engineering ConsultantCommented:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.