Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Save files change history?

Posted on 2009-12-17
6
Medium Priority
?
158 Views
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:

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
 
0
Comment
Question by:ST3VO
  • 3
  • 3
6 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 total points
ID: 26075622
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
 

Author Comment

by:ST3VO
ID: 26076015
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
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 26076107
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:ST3VO
ID: 26076214
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
 
LVL 23

Expert Comment

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

Author Closing Comment

by:ST3VO
ID: 31667489
Great! Thanks :o)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 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