Recent

Author Topic: [SOLVED] How do I change the file created date?  (Read 12051 times)

bzman24

  • Jr. Member
  • **
  • Posts: 71
[SOLVED] How do I change the file created date?
« on: August 20, 2014, 07:23:00 pm »
Hello,

I have several hundred audio files that have the file created date, file modified date and file accessed date all set at 01/01/1980 12:00:00 AM.  The filenames are all of the format: 010814-152848.WAV as an example.  I want to be able to change the created and modified dates to the date and time as given in the filename.  I have no problem converting the filename to the proper format of date and time as needed by FileSetDate, but this only changes the file modified date.  How do I change the created date also?

I have found that the (SearchRec.FindData.ftCreationTime, DT) shows the created time but can I use that in something like (SearchRec.PutData.ftCreationTime, DT) (as an example I tried this but it doesn't work) to write the created date?

I think I remember back in the Turbo Pascal 6 days, you could use a TSearchRec to read and or write file attributes.  I have searched all over on the internet on this but can't find anything.  I know it can be done programmatically because there are several freeware programs out there that will do this but I have to type all of the dates and times in manually.  Is this only available in other languages like maybe C or one of those derivatives and not Pascal?

Thanks for any help on this.

BZman

=======================================
Lazarus version: 1.2.4
Lazarus svn revision: 45510
Lazarus build date: 2014/06/14
Lazarus was compiled for i386-win32
Lazarus was compiled with fpc 2.6.4

Running on a Win7 desktop, 3 Gig of memory.
=======================================
« Last Edit: August 25, 2014, 07:05:29 pm by bzman24 »
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How do I change the file created date?
« Reply #1 on: August 20, 2014, 10:44:42 pm »
FileSetDate() perhaps?

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How do I change the file created date?
« Reply #2 on: August 20, 2014, 11:18:35 pm »
To set the three dates (creation, last accessed, last modified) you have to use the Windows API SetFileTime call, for which a Pascal header is provided in the Windows unit. AFAICS there is no Pascal header in the RTL for 64-bit SetFileTime() (but I may have missed it).

Code: [Select]
function SetFileTime(hFile:HANDLE; var lpCreationTime:FILETIME; var lpLastAccessTime:FILETIME;
                             var lpLastWriteTime:FILETIME):WINBOOL; external 'kernel32' name 'SetFileTime';
« Last Edit: August 20, 2014, 11:20:32 pm by howardpc »

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I change the file created date?
« Reply #3 on: August 21, 2014, 12:38:42 am »
Bart,  I tried that as explained above.  That works for file modified date only.  Thanks.

Howardpc,  That looks like something I can use.  However when I add "Windows" to the USES clauses it gives me errors in other parts of the code that weren't there before.  I guess that is not how to do it.  Any chance of a small example of how to use that call in Pascal?

Thanks,

BZman
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How do I change the file created date?
« Reply #4 on: August 21, 2014, 01:45:09 am »
The following main form unit example has a TButton and a TOpenDialog dropped on it.

Code: [Select]
unit mainSetFileTime;

{$mode objfpc}{$H+}

interface

uses
  windows, Classes, SysUtils, FileUtil, Forms, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    BSetFileTime: TButton;
    OpenDialog1: TOpenDialog;
    procedure BSetFileTimeClick(Sender: TObject);
  private
    function SetFileDate(const aFilename: string; aDate: TDateTime): boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.BSetFileTimeClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if SetFileDate(OpenDialog1.FileName, Now) then
      ShowMessageFmt('All OS file dates of %s are set to this moment''s date and time',
                     [OpenDialog1.FileName])
    else ShowMessage('SetFileDate() function failed');
end;

function TForm1.SetFileDate(const aFilename: string; aDate: TDateTime): boolean;
var
  fileHandle: THandle;
  fileTime: TFILETIME;
  LFileTime: TFILETIME;
  LSysTime: TSystemTime;
begin
  Result:=False;
  try
    DecodeDate(aDate, LSysTime.Year, LSysTime.Month, LSysTime.Day);
    DecodeTime(aDate, LSysTime.Hour, LSysTime.Minute, LSysTime.Second, LSysTime.Millisecond);
    if SystemTimeToFileTime(LSysTime, LFileTime) then
    begin
      if LocalFileTimeToFileTime(LFileTime, fileTime) then
      begin
        fileHandle:=FileOpenUTF8(aFilename, fmOpenReadWrite or fmShareExclusive);
        if SetFileTime(fileHandle, fileTime, fileTime, fileTime) then
          Result:=True;
      end;
    end;
  finally
    FileClose(fileHandle);
  end;
end;

end.

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I change the file created date?
« Reply #5 on: August 21, 2014, 04:22:44 am »
Thanks.  Its late here now, so I won't get to try this till tomorrow.  I'll try it and let you know.  Thanks again.

BZman
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I change the file created date?
« Reply #6 on: August 21, 2014, 07:49:06 pm »
howardpc,

That does what I need.  Now I have to figure out whether I can incorporate this code into my existing project or maybe I can rebuild my project around your sample code.  I am using a CheckListBox and filling it with a TSearchRec FindFirst, FindNext procedure.  The Windows unit produces errors in the TSearchRec procedure.  So I guess I will have to do the file search method differently.

Thanks again,

BZman :D
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: How do I change the file created date?
« Reply #7 on: August 21, 2014, 09:11:49 pm »

Howardpc,  That looks like something I can use.  However when I add "Windows" to the USES clauses it gives me errors in other parts of the code that weren't there before. 

BZman
I run into this a lot as well.  just create a separate "utility" unit and include "windows" and whatever else you need there. 
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: How do I change the file created date?
« Reply #8 on: August 21, 2014, 10:04:57 pm »
I am using a CheckListBox and filling it with a TSearchRec FindFirst, FindNext procedure.  The Windows unit produces errors in the TSearchRec procedure.  So I guess I will have to do the file search method differently.
The unit Windows and Sysutils both have FindFirst, FindNext and FindClose procedures (but need different parameters). So you need to put Windows before the SysUtils in the uses clause. That way the Sysutils procedures are taken (which expects a TSearchRec from that unit). Otherwise you can also prepend SysyUtils. to the function calls. (i.e. SysUtils.FindClose(A); etc.). For existing units it's easier to change the uses order but is't more clear in code to use Sysutils. before your call.
« Last Edit: August 21, 2014, 10:09:35 pm by rvk »

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I change the file created date?
« Reply #9 on: August 22, 2014, 02:42:19 am »
Thanks guys for the suggestions.  rvk - I think the fiddling with the uses clauses orders and adding the 'SysUtils.' example you give might be the way to go.  I'll try these things and see what I can get to work.  Might take me a day or so to have the time to do it.

BZman
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How do I change the file created date?
« Reply #10 on: August 22, 2014, 09:36:26 am »
 Double Commander supports changing of file attributes in the Windows version, if it does in the Linux one, too, solution could be taken from there.
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: How do I change the file created date?
« Reply #11 on: August 25, 2014, 07:04:27 pm »
Thanks everybody for the help and the examples.  I got it working as I needed by incorporating the example code in to my project and modifing it as needed.

BZman :D :D
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

 

TinyPortal © 2005-2018