Recent

Author Topic: Setting Creation Date and Modified Date - How To?  (Read 798 times)

zxandris

  • Jr. Member
  • **
  • Posts: 67
Setting Creation Date and Modified Date - How To?
« on: February 12, 2024, 11:47:41 am »
I've searched the forum and the entire internet and I've had no luck in trying to set the file creation date, and the modified date.  function FileSetDateUTF8 should work but it doesn't actually appear to change anything and I want to change both the file age and the modified to different date times.

I found :-
Code: Pascal  [Select][+][-]
  1. function SetFileDate(const aFilename: string; aDate: TDateTime): boolean;
  2. var
  3.   fileHandle: THandle;
  4.   fileTime: TFILETIME;
  5.   LFileTime: TFILETIME;
  6.   LSysTime: TSystemTime;
  7. begin
  8.   Result:=False;
  9.   try
  10.     DecodeDate(aDate, LSysTime.Year, LSysTime.Month, LSysTime.Day);
  11.     DecodeTime(aDate, LSysTime.Hour, LSysTime.Minute, LSysTime.Second, LSysTime.Millisecond);
  12.     if SystemTimeToFileTime(LSysTime, LFileTime) then
  13.     begin
  14.       if LocalFileTimeToFileTime(LFileTime, fileTime) then
  15.       begin
  16.         fileHandle:=FileOpenUTF8(aFilename, fmOpenReadWrite or fmShareExclusive);
  17.         if SetFileTime(fileHandle, fileTime, fileTime, fileTime) then
  18.           Result:=True;
  19.       end;
  20.     end;
  21.   finally
  22.     FileClose(fileHandle);
  23.   end;
  24. end;
  25.  

That seems to just blank the modified date, (reporting in windows).

But I have no idea how to make that work either, windows calls are not my forte.  Any one would like to take pity on a noob? 

zxandris

  • Jr. Member
  • **
  • Posts: 67
Re: Setting Creation Date and Modified Date - How To?
« Reply #1 on: February 12, 2024, 11:57:53 am »
Actually I Think I have it doing what I want but I would REALLY appreciate someone checking it for me.  It seems to set the date right, now sure about the time.

Code: Pascal  [Select][+][-]
  1. function SetFileDate(const aFilename: string; ccdate, mDate: TDateTime): boolean;
  2. var
  3.   fileHandle: THandle;
  4.   NewCreationFileTime: TFILETIME;
  5.   LFileTime: TFILETIME;
  6.   LSysTime: TSystemTime;
  7.   mfileTime: TFILETIME;
  8.   mLFileTime: TFILETIME;
  9.   mLSysTime: TSystemTime;
  10. begin
  11.   Result:=False;
  12.   try
  13.     DecodeDate(mDate, LSysTime.Year, LSysTime.Month, LSysTime.Day);
  14.     DecodeTime(mDate, LSysTime.Hour, LSysTime.Minute, LSysTime.Second, LSysTime.Millisecond);
  15.     DecodeDate(ccDate, mlSysTime.Year, mlSysTime.Month, mlSysTime.Day);
  16.     DecodeTime(ccDate, mlSysTime.Hour, mlSysTime.Minute, mlSysTime.Second, mlSysTime.Millisecond);
  17.     if SystemTimeToFileTime(mlSysTime, mFileTime) then
  18.     begin
  19.         if LocalFileTimeToFileTime(mFileTime, mLFileTime) then
  20.         begin
  21.             if SystemTimeToFileTime(LSysTime, LFileTime) then
  22.             begin
  23.               if LocalFileTimeToFileTime(LFileTime, NewCreationFileTime) then
  24.               begin
  25.                 fileHandle:=FileOpenUTF8(aFilename, fmOpenReadWrite or fmShareExclusive);
  26.                 if SetFileTime(fileHandle, NewCreationFileTime, mfileTime, NewCreationFileTime) then
  27.                   Result:=True;
  28.               end;
  29.             end;
  30.         end;
  31.     end;
  32.   finally
  33.     FileClose(fileHandle);
  34.   end;
  35. end;
  36.  

Please help a noob here, I have to admit I don't especilly know what the code is doing so that doesn't help - lol.

zxandris

  • Jr. Member
  • **
  • Posts: 67
Re: Setting Creation Date and Modified Date - How To?
« Reply #2 on: February 12, 2024, 12:08:50 pm »
Actually I Think I have it doing what I want but I would REALLY appreciate someone checking it for me.  It seems to set the date right, now sure about the time.

Code: Pascal  [Select][+][-]
  1. function SetFileDate(const aFilename: string; ccdate, mDate: TDateTime): boolean;
  2. var
  3.   fileHandle: THandle;
  4.   NewCreationFileTime: TFILETIME;
  5.   LFileTime: TFILETIME;
  6.   LSysTime: TSystemTime;
  7.   mfileTime: TFILETIME;
  8.   mLFileTime: TFILETIME;
  9.   mLSysTime: TSystemTime;
  10. begin
  11.   Result:=False;
  12.   try
  13.     DecodeDate(mDate, LSysTime.Year, LSysTime.Month, LSysTime.Day);
  14.     DecodeTime(mDate, LSysTime.Hour, LSysTime.Minute, LSysTime.Second, LSysTime.Millisecond);
  15.     DecodeDate(ccDate, mlSysTime.Year, mlSysTime.Month, mlSysTime.Day);
  16.     DecodeTime(ccDate, mlSysTime.Hour, mlSysTime.Minute, mlSysTime.Second, mlSysTime.Millisecond);
  17.     if SystemTimeToFileTime(mlSysTime, mFileTime) then
  18.     begin
  19.         if LocalFileTimeToFileTime(mFileTime, mLFileTime) then
  20.         begin
  21.             if SystemTimeToFileTime(LSysTime, LFileTime) then
  22.             begin
  23.               if LocalFileTimeToFileTime(LFileTime, NewCreationFileTime) then
  24.               begin
  25.                 fileHandle:=FileOpenUTF8(aFilename, fmOpenReadWrite or fmShareExclusive);
  26.                 if SetFileTime(fileHandle, NewCreationFileTime, mfileTime, NewCreationFileTime) then
  27.                   Result:=True;
  28.               end;
  29.             end;
  30.         end;
  31.     end;
  32.   finally
  33.     FileClose(fileHandle);
  34.   end;
  35. end;
  36.  

Please help a noob here, I have to admit I don't especilly know what the code is doing so that doesn't help - lol.

Nope still not working right, it seems to set the date, but the time is weird, and for some reason it doesn't report anything when you look at the file in windows export, the modified is blank but under properties it's set the date, but not the time.

Thaddy

  • Hero Member
  • *****
  • Posts: 14014
  • Probably until I exterminate Putin.
Re: Setting Creation Date and Modified Date - How To?
« Reply #3 on: February 12, 2024, 01:17:04 pm »
If it is not the filename on disk but the real creation/compilation date you can use Attributes in trunk. There is an example in the wiki that I added.
This example stores the creation/compile date without relying on the disk date, so it avoids being fooled by copying..
https://wiki.freepascal.org/Custom_Attributes#Complete_example

As long as you do not recompile, that date is static and so the real compilation date..
« Last Edit: February 12, 2024, 01:31:16 pm by Thaddy »
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1999
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting Creation Date and Modified Date - How To?
« Reply #4 on: February 12, 2024, 05:09:29 pm »
Attached is a demo that full covers the usage for Windows API GetFileTime() and SetFileTime().

It does currently not work when target file is in use or locked.

Enjoy!

Ps: just written in a hurry so not much checks done yet.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1999
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting Creation Date and Modified Date - How To?
« Reply #5 on: February 12, 2024, 08:14:28 pm »
Quick update:
Please add/update the helpers:
Code: Pascal  [Select][+][-]
  1. function TForm1.EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: word): TDateTime;
  2. var
  3.   LDateTime: TDateTime;
  4. begin
  5.   SetLastError(ERROR_SUCCESS);
  6.   Result := Now;
  7.   try
  8.     LDateTime := Trunc(EncodeDate(AYear, AMonth, ADay)) +
  9.                  Frac(EncodeTime(AHour, AMinute, ASecond, AMillisecond));
  10.   finally
  11.     Result := LDateTime;
  12.   end;
  13. end;
  14.  
  15. function TForm1.FileTimeToLocalSystemTime(const AFileTime: TFileTime): TSystemTime;
  16. var
  17.   LLocalTime: TFileTime;
  18. begin
  19.   SetLastError(ERROR_SUCCESS);
  20.   try
  21.     FileTimeToLocalFileTime(AFileTime, LLocalTime);
  22.   finally
  23.     FileTimeToSystemTime(LLocalTime, Result);
  24.   end;
  25. end;
  26.  
  27. function TForm1.FileTimeToDateTime(const AFileTime: TFileTime): TDateTime;
  28. var
  29.   LSystemTime: TSystemTime;
  30. begin
  31.   SetLastError(ERROR_SUCCESS);
  32.   try
  33.     LSystemTime := FileTimeToLocalSystemTime(AFileTime);
  34.   finally
  35.     with LSystemTime do
  36.       Result := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
  37.   end;
  38. end;
  39.  
  40. function TForm1.DateTimeToFileTime(const ADateTime: TDateTime): TFileTime;
  41. var
  42.   LFileTime: TFileTime;
  43.   LSystemTime: TSystemTime;
  44. begin
  45.   SetLastError(ERROR_SUCCESS);
  46.   try
  47.     with LSystemTime do
  48.       begin
  49.         DecodeDate(ADateTime, wYear, wMonth, wDay);
  50.         DecodeTime(ADateTime, wHour, wMinute, wSecond, wMilliSeconds);
  51.       end;
  52.   finally
  53.     if SystemTimeToFileTime(LSystemTime, LFileTime) then
  54.       LocalFileTimeToFileTime(LFileTime, Result);
  55.   end;
  56. end;
and in "function TForm1.SetFileTimes: Boolean;" update the LNew* settings to:
Code: Pascal  [Select][+][-]
  1.   FNewCreate := EncodeDateTime(seYearCreate.Value, seMonthCreate.Value, seDayCreate.Value, seHourCreate.Value, seMinuteCreate.Value, seSecondCreate.Value, seMillisecondCreate.Value);
  2.   FNewModify := EncodeDateTime(seYearModified.Value, seMonthModified.Value, seDayModified.Value, seHourModified.Value, seMinuteModified.Value, seSecondModified.Value, seMillisecondModified.Value);
  3.   FNewAccess := EncodeDateTime(seYearAccess.Value, seMonthAccess.Value, seDayAccess.Value, seHourAccess.Value, seMinuteAccess.Value, seSecondAccess.Value, seMillisecondAccess.Value);
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zxandris

  • Jr. Member
  • **
  • Posts: 67
Re: Setting Creation Date and Modified Date - How To?
« Reply #6 on: February 13, 2024, 04:20:03 pm »
Thank you, that's very kind of you to help I'll have to see what I can do with this - thanks :)

CJ

Quick update:
Please add/update the helpers:
Code: Pascal  [Select][+][-]
  1. function TForm1.EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: word): TDateTime;
  2. var
  3.   LDateTime: TDateTime;
  4. begin
  5.   SetLastError(ERROR_SUCCESS);
  6.   Result := Now;
  7.   try
  8.     LDateTime := Trunc(EncodeDate(AYear, AMonth, ADay)) +
  9.                  Frac(EncodeTime(AHour, AMinute, ASecond, AMillisecond));
  10.   finally
  11.     Result := LDateTime;
  12.   end;
  13. end;
  14.  
  15. function TForm1.FileTimeToLocalSystemTime(const AFileTime: TFileTime): TSystemTime;
  16. var
  17.   LLocalTime: TFileTime;
  18. begin
  19.   SetLastError(ERROR_SUCCESS);
  20.   try
  21.     FileTimeToLocalFileTime(AFileTime, LLocalTime);
  22.   finally
  23.     FileTimeToSystemTime(LLocalTime, Result);
  24.   end;
  25. end;
  26.  
  27. function TForm1.FileTimeToDateTime(const AFileTime: TFileTime): TDateTime;
  28. var
  29.   LSystemTime: TSystemTime;
  30. begin
  31.   SetLastError(ERROR_SUCCESS);
  32.   try
  33.     LSystemTime := FileTimeToLocalSystemTime(AFileTime);
  34.   finally
  35.     with LSystemTime do
  36.       Result := EncodeDateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
  37.   end;
  38. end;
  39.  
  40. function TForm1.DateTimeToFileTime(const ADateTime: TDateTime): TFileTime;
  41. var
  42.   LFileTime: TFileTime;
  43.   LSystemTime: TSystemTime;
  44. begin
  45.   SetLastError(ERROR_SUCCESS);
  46.   try
  47.     with LSystemTime do
  48.       begin
  49.         DecodeDate(ADateTime, wYear, wMonth, wDay);
  50.         DecodeTime(ADateTime, wHour, wMinute, wSecond, wMilliSeconds);
  51.       end;
  52.   finally
  53.     if SystemTimeToFileTime(LSystemTime, LFileTime) then
  54.       LocalFileTimeToFileTime(LFileTime, Result);
  55.   end;
  56. end;
and in "function TForm1.SetFileTimes: Boolean;" update the LNew* settings to:
Code: Pascal  [Select][+][-]
  1.   FNewCreate := EncodeDateTime(seYearCreate.Value, seMonthCreate.Value, seDayCreate.Value, seHourCreate.Value, seMinuteCreate.Value, seSecondCreate.Value, seMillisecondCreate.Value);
  2.   FNewModify := EncodeDateTime(seYearModified.Value, seMonthModified.Value, seDayModified.Value, seHourModified.Value, seMinuteModified.Value, seSecondModified.Value, seMillisecondModified.Value);
  3.   FNewAccess := EncodeDateTime(seYearAccess.Value, seMonthAccess.Value, seDayAccess.Value, seHourAccess.Value, seMinuteAccess.Value, seSecondAccess.Value, seMillisecondAccess.Value);

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1999
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting Creation Date and Modified Date - How To?
« Reply #7 on: February 13, 2024, 08:05:38 pm »
Updated version attached.
Now UI and the number of possible days in a month do match.
« Last Edit: February 13, 2024, 08:07:16 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

 

TinyPortal © 2005-2018