Save files change history?

Posted on 2009-12-17
Last Modified: 2012-05-08
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:

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.


Question by:ST3VO
    LVL 22

    Accepted Solution

    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
      Rec: TSearchRec;
      FileList: array of string;
      DateList: array of TDateTime;
      i: Integer;
      Done: Boolean;
      TempName: string;
      TempDate: TDateTime;
      if FindFirst(BaseName, faAnyFile, Rec) = 0 then
          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;
      // Bubble sort
        Done := True;
        for i := 0 to High(FileList) - 1 do
          if DateList[i] < DateList[i + 1] then
            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;
      until Done;
      result := FileList[0]; //here we'll have the most recent file
    Now you can open your file calling it passing as Basename 'c:\somedir\Project1*.*' 

    Open in new window


    Author Comment

    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:


    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?

    LVL 22

    Expert Comment

    by:Ferruccio Accalai
    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

    Author Comment

    Hmm...I just don't get the call example..

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

    Is it something like:

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



    LVL 22

    Expert Comment

    by:Ferruccio Accalai
    OpenMostRecent is a dummy function that i've hoped you might have created.
    Forget that :)
    Well, you should finally use this method:


    Of course change TheDirWhereYouAreSaving with the right directory where the files are stored

    Author Closing Comment

    Great! Thanks :o)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now