Save files change history?

Hi all,

Suppose I saved the contents of a tmemo as a text file ....

Then another 30 minutes later (just an example)

Then my computer freezes and a lost my current work ...

How can I open the latest version saved by code?

I could save the filename with a date/time in the filename but how do I get the code to know which one it has to open without me having to check the datetime in the filename?

For example ... if the file is called:

project1
then I'd like to add a "-" (without the quotes) and then the datetime stamp included in the filename...

Then if I open Project1  .... the code checks the latest and opens that one....

Hope this makes sense.

I'm opened the suggestions if this is not the best way to do it.

thanks

st3vo
 
ST3VOAsked:
Who is Participating?
 
Ferruccio AccalaiConnect With a Mentor Senior developer, analyst and customer assistance Commented:
Let's see.

You could search for the most recent file and open it regardless of its name.
To do this we can use a TSearchRec to enumerating all the files in the directory where you're saving the text files, depending on their name (let's say a basename like Project1) and retrieving their creation time.
Then, using a bubble sort to sort the files by descending datetime we can know wich is the most recent and open it.

See the code for an example.
function GetMostRecent(BaseName: String): String
var
  Rec: TSearchRec;
  FileList: array of string;
  DateList: array of TDateTime;
  i: Integer;
  Done: Boolean;
  TempName: string;
  TempDate: TDateTime;
begin
  if FindFirst(BaseName, faAnyFile, Rec) = 0 then
    repeat
      Setlength(FileList, Length(FileList) + 1);
      Setlength(DateList, Length(DateList) + 1);
      FileList[ High(FileList)] := ExtractFilePath(BaseName)+Rec.Name;
      DateList[ High(DateList)] := FileDateToDateTime(Rec.Time);
    until FindNext(Rec) <> 0;
    FindClose(Rec);
  // Bubble sort
  repeat
    Done := True;
    for i := 0 to High(FileList) - 1 do
      if DateList[i] < DateList[i + 1] then
      begin
        Done := False;
        TempName := FileList[i];
        FileList[i] := FileList[i + 1];
        FileList[i + 1] := TempName;
        TempDate := DateList[i];
        DateList[i] := DateList[i + 1];
        DateList[i + 1] := TempDate;
      end;
  until Done;
  result := FileList[0]; //here we'll have the most recent file
end;

Now you can open your file calling it passing as Basename 'c:\somedir\Project1*.*' 

i.e.
 OpenMostRecent(GetMostRecent('c:\somedir\Project1*.*'));

Open in new window

0
 
ST3VOAuthor Commented:
Hmmm...Almost perfect but I cannot rely on the sorting....  The first search on the Filename ...

e.g: Project1 etc.... The filename could have a "-" after the main name to help .....

So I could have:

Project1-somethinghere.txt
Project1-somethingelse.txt
etc...

Then I select to open Project1 and the code reads Everything before the "-" (for example) and then opens the newest one depending on datetime stamp for example?

You know what I'm after?

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
In the code I've posted you can open the last created Project1-whatever just using the form 'c:\somedir\Project1*.*'
This means that the TSearchRec use Project1*.* as filter, then the bubble sort return the most recent, that is the newest
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ST3VOAuthor Commented:
Hmm...I just don't get the call example..

OpenMostRecent(GetMostRecent('c:\somedir\Project1*.*')); ....

Is it something like:

Var thefile:string;
begin
thefile :=OpenMostRecent(GetMostRecent('c:\somedir\Project1*.*'));

memo1.lines.loadfromfile(thefile);

????

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
OpenMostRecent is a dummy function that i've hoped you might have created.
Forget that :)
Well, you should finally use this method:

Memo1.lines.loadfromfile(GetMostRecent('c:\TheDirWhereYouAreSaving\Project1*.*));

Of course change TheDirWhereYouAreSaving with the right directory where the files are stored
0
 
ST3VOAuthor Commented:
Great! Thanks :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.