Recent

Author Topic: Check file date and replace.  (Read 7511 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
Check file date and replace.
« on: March 02, 2016, 12:53:48 am »
I'm creating a small prog that acts as an updater/starter for an online game.
There are 3 files that can change frequently and I want the starter to compare a client's local files and, if older/different, replace the client files with the newer/correct files.
I've been looking at fileutil but only see the ability to see if a file exists and not compare.

I thought "file time" might be an option and found this code from another thread here. Is it possible to use this and, if so, how ?

Code: Pascal  [Select][+][-]
  1. function getcreationtime(fn:string;var v: TDateTime):Boolean;
  2. var fl:TSearchRec;
  3. begin
  4.   result:=GetFileInformation(fn,fl);
  5.   if result then
  6.     v:=filetimetodatetime(fl.FindData.ftCreationTime);
  7. end;
  8.  

Long term I want to make this into an auto updater.
I'm using HttpSend to retrieve files from remote server and save them locally.

Any help much appreciated.

balazsszekely

  • Guest
Re: Check file date and replace.
« Reply #1 on: March 02, 2016, 07:24:56 am »
Use the following function to get the filedate as TDateTime:

Code: Pascal  [Select][+][-]
  1. uses windows;
  2.  
  3. function GetFileTime(const AFileName: String; var FileTime: TDateTime): Boolean;
  4. var
  5.   SR: TSearchRec;
  6.   LocalFileTime: TFileTime;
  7.   SystemTime: TSystemTime;
  8. begin
  9.   Result := False;
  10.   if FindFirst(AFileName, faAnyFile, SR) = 0 then
  11.   begin
  12.     if FileTimeToLocalFileTime(SR.FindData.ftCreationTime, LocalFileTime) then
  13.     begin
  14.       if FileTimeToSystemTime(LocalFileTime, SystemTime) then
  15.        begin
  16.          FileTime := SystemTimeToDateTime(SystemTime);
  17.          Result := True;
  18.        end;
  19.     end;
  20.     SysUtils.FindClose(SR);
  21.   end;
  22. end;
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. var
  26.   FileTime: TDateTime;
  27. begin
  28.   if GetFileTime('d:\test.txt', FileTime) then
  29.     ShowMessage(FormatDateTime('YYYY.MM.DD hh:mm:ss', FileTime));
  30. end;  

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Check file date and replace.
« Reply #2 on: March 02, 2016, 06:17:32 pm »
Thanks for that.
Something for me to work with.

Just wondered why copying the code brings in the line numbers and whitespace ?

balazsszekely

  • Guest
Re: Check file date and replace.
« Reply #3 on: March 02, 2016, 06:23:52 pm »
Quote
Thanks for that.
Something for me to work with.
You're welcome!
Quote
Just wondered why copying the code brings in the line numbers and whitespace ?
It does not, I just test it!

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Check file date and replace.
« Reply #4 on: March 02, 2016, 08:59:37 pm »
Oh yes it does !

I get it if I copy manually or use the select option.
It pastes numbers onto here and directly into Laz 1.6.
I'm using Win 7 and IE 11.
Just tested it with Chrome too, same result.

Code: Pascal  [Select][+][-]
  1.  
  2. uses windows;
  3.  
  4.  
  5. 2.
  6.  
  7.  
  8. 3.function GetFileTime(const AFileName: String; var FileTime: TDateTime): Boolean;
  9.  
  10.  
  11. 4.var
  12.  
  13.  
  14. 5.  SR: TSearchRec;
  15.  
  16.  
  17. 6.  LocalFileTime: TFileTime;
  18.  
  19.  
  20. 7.  SystemTime: TSystemTime;
  21.  
  22.  
  23. 8.begin
  24.  
  25.  
  26. 9.  Result := False;
  27.  
  28.  
  29. 10.  if FindFirst(AFileName, faAnyFile, SR) = 0 then
  30.  
  31.  
  32. 11.  begin
  33.  
  34.  
  35. 12.    if FileTimeToLocalFileTime(SR.FindData.ftCreationTime, LocalFileTime) then
  36.  
  37.  
  38. 13.    begin
  39.  
  40.  
  41. 14.      if FileTimeToSystemTime(LocalFileTime, SystemTime) then
  42.  
  43.  
  44. 15.       begin
  45.  
  46.  
  47. 16.         FileTime := SystemTimeToDateTime(SystemTime);
  48.  
  49.  
  50. 17.         Result := True;
  51.  
  52.  
  53. 18.       end;
  54.  
  55.  
  56. 19.    end;
  57.  
  58.  
  59. 20.    SysUtils.FindClose(SR);
  60.  
  61.  
  62. 21.  end;
  63.  
  64.  
  65. 22.end;
  66.  
  67.  
  68. 23.
  69.  
  70.  
  71. 24.procedure TForm1.Button1Click(Sender: TObject);
  72.  
  73.  
  74. 25.var
  75.  
  76.  
  77. 26.  FileTime: TDateTime;
  78.  
  79.  
  80. 27.begin
  81.  
  82.  
  83. 28.  if GetFileTime('d:\test.txt', FileTime) then
  84.  
  85.  
  86. 29.    ShowMessage(FormatDateTime('YYYY.MM.DD hh:mm:ss', FileTime));
  87.  
  88.  
  89. 30.end;  
  90.  
  91.  
« Last Edit: March 02, 2016, 09:03:47 pm by Zath »

eny

  • Hero Member
  • *****
  • Posts: 1651
Re: Check file date and replace.
« Reply #5 on: March 02, 2016, 11:35:21 pm »
Oh yes it does !
I recall having the same problem in the past, but not anymore.
Could be Window$ (IE version) related.
With Win10 the copy/paste works as expected.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

balazsszekely

  • Guest
Re: Check file date and replace.
« Reply #6 on: March 03, 2016, 07:46:13 am »
@eny
Yes, I was able to reproduce with the crappy IE 9.0, but who works with IE nowadays?

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: Check file date and replace.
« Reply #7 on: March 03, 2016, 10:01:38 am »
@eny
Yes, I was able to reproduce with the crappy IE 9.0, but who works with IE nowadays?

I use IE 11, but, as I said in my earlier post, it also happens with Chrome.
It's not much of a problem when copying a small procedure but a larger piece of code would be more difficult.

balazsszekely

  • Guest
Re: Check file date and replace.
« Reply #8 on: March 03, 2016, 10:46:07 am »
Quote
@Zath
I use IE 11, but, as I said in my earlier post, it also happens with Chrome.
It's not much of a problem when copying a small procedure but a larger piece of code would be more difficult.
Can somebody else confirm this with Chrome, Mozilla, etc..(not IE)?

eny

  • Hero Member
  • *****
  • Posts: 1651
Re: Check file date and replace.
« Reply #9 on: March 03, 2016, 05:55:09 pm »
Can somebody else confirm this with Chrome, Mozilla, etc..(not IE)?
Works as expected on Win10 + Firefox.
« Last Edit: March 04, 2016, 02:40:43 am by JuhaManninen »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Check file date and replace.
« Reply #10 on: March 03, 2016, 09:07:29 pm »
Hmmm, funny...

Works fine with Firefox 33 (NoScript On and Off) and Akelpad and Lazarus 1.4.2....

 

TinyPortal © 2005-2018