views:

328

answers:

1

I have a program that imports a text file that has many entrys:

###
Starttime: 06.03.2008
Data: SOME RECORDS HERE

###
Starttime: 21.03.2008
Data SOME RECORDS HERE

... and so on

Not I want to have an end time after "Data:" that is the next starttime -1 so i have

###
Starttime: 06.03.2008
Data: SOME RECORDS HERE
EndTime: 20.03.2008

###
Starttime: 21.03.2008
Data SOME RECORDS HERE
EndTime: (next starttime -1)

... and so on

+3  A: 

The easiest way would be to read the file into a TStringList and work there.

Pseudocode:

var 
  S: TStringList;
  i: Integer;
  LastDate: TDateTime;
  CurDate: TDateTime;
begin
  S := TStringList.Create;
  S.LoadFromFile('c:\...');

  i := 0;
  while i < S.Count do
  begin
    if S[i] = "###" then
    begin
      CurDate := StrToDate(S[i+1])
      S.Insert(i-3, DateToStr(CurDate));
      LastDate := CurDate;
      i := i+2;
    end else
    begin
      i := i+1;
    end;
  end;
  S.SaveToFile('c:\...');
end;

This code is not very robust, it doesn't check for any special cases, like the first starting date, but it should be enough to get you started.

DR
The problem with this approach is that it requires the amount of memory to match that of the new file. reading to one file and writing to another would be much more efficient as you only have to visit each line once and would be able to handle any size file as long as there was disk space.
skamradt