Recent

Author Topic: [SOLVED] How do you update a file timestamp withoug opening it?  (Read 289 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 325
This bit of code doesn't work. Suggestions?
Code: Pascal  [Select][+][-]
  1. Procedure Touch(const aFileName: String);
  2. var f : File;
  3. begin
  4.   If Not FileExists(aFileName) then exit;
  5.   // Try to open the file to update its timestamp
  6.    AssignFile(f, aFileName);
  7.    {$I-} // Disable I/O checking
  8.    Reset(f);
  9.    {$I+} // Enable I/O checking
  10.   //Close it to update the timestamp
  11.   CloseFile(f);
  12. end;
  13.  

« Last Edit: May 25, 2025, 09:56:39 am by EganSolo »

dsiders

  • Hero Member
  • *****
  • Posts: 1438
Re: How do you update a file timestamp withoug opening it?
« Reply #1 on: May 25, 2025, 05:51:25 am »
This bit of code doesn't work. Suggestions?
Code: Pascal  [Select][+][-]
  1. Procedure Touch(const aFileName: String);
  2. var f : File;
  3. begin
  4.   If Not FileExists(aFileName) then exit;
  5.   // Try to open the file to update its timestamp
  6.    AssignFile(f, aFileName);
  7.    {$I-} // Disable I/O checking
  8.    Reset(f);
  9.    {$I+} // Enable I/O checking
  10.   //Close it to update the timestamp
  11.   CloseFile(f);
  12. end;
  13.  

I don't thibk CloseFile() is going to flush anything to disk... because nothing was written.
Why don't you try using sysutils.FileSetDate() with the current timestamp (converted using DateTimeToFileDate)?
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

EganSolo

  • Sr. Member
  • ****
  • Posts: 325
Re: [SOLVED] How do you update a file timestamp withoug opening it?
« Reply #2 on: May 25, 2025, 09:57:05 am »
dsiders,

  Thank you! That did it.

 

TinyPortal © 2005-2018