Get and set a file's date and time stamp

vegaseat 0 Tallied Votes 528 Views Share

You can look at a selected file's date and time stamp and change it with this Delphi program. XP files need a little extra TLC, but it's taken care of too.

// to get and set a file's date and time stamp
// use  FileGetDate() and FileSetDate()
// also using  "fmOpenReadWrite or fmShareDenyWrite" in
// FileOpen() makes FileSetDate() work in Windows XP
// experiments with Delphi by vegaseat

unit Fdatez;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    OpenFileButton: TButton;
    FirstCharLabel: TLabel;
    FileDateIntLabel: TLabel;
    FileDateStrLabel: TLabel;
    ASCIIValLabel: TLabel;
    Edit1: TEdit;
    ChangeButton: TButton;
    Label5: TLabel;
    FHandleLabel: TLabel;
    FileNameLabel: TLabel;
    SuccessLabel: TLabel;
    procedure OpenFileButtonClick(Sender: TObject);
    procedure ChangeButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.OpenFileButtonClick(Sender: TObject);
var
  F: TextFile;
  FH: Integer;
  FirstChar: Char;
  FDate: LongInt;
  FDateTime: TDateTime;
  DateTimeStr: String;
begin
  with OpenDialog1 do
  begin
    if Execute then
    begin
      AssignFile(F, FileName);
      Reset(F);
      Read(F, FirstChar);
      // display the first character of the file
      FirstCharLabel.Caption := FirstChar;
      // just for kicks, display the ASCII value of the first character
      // for the curious  .jpg = 255   .tif = 73   .bmp = 66   .gif = 71
      ASCIIValLabel.Caption := IntToStr(Ord(FirstChar));
      // close the file with the counterpart of AssignFile()
      CloseFile(F);

      // get file handle, use fmOpenRead for mode
      FileNameLabel.Caption := FileName;
      FH := FileOpen(FileName, fmOpenRead);
      FHandleLabel.Caption := IntToStr(FH);
      if FH > 0 then
      begin
        FDate := FileGetDate(FH);
        FileDateIntLabel.Caption := IntToStr(FDate);
        if FDate > 0 then
        begin
          FDateTime := FileDateToDateTime(Fdate);
          DateTimeStr := DateTimeToStr(FDateTime);
          // display the actual date time string
          FileDateStrLabel.Caption := DateTimeStr;
        end;
      end;
      FileClose(FH);
    end;
  end;
end;


procedure TForm1.ChangeButtonClick(Sender: TObject);
var
  FH, success: Integer;
  FDate: LongInt;
  FDateTime: TDateTime;
begin
  // get file handle, use fmInput for mode
  FH := FileOpen(OpenDialog1.FileName, fmOpenReadWrite or fmShareDenyWrite);
  FDateTime := StrToDateTime(Edit1.Text);
  FDate := DateTimeToFileDate(FDateTime);
  // change the date time stamp of the file
  //  returns 0 on success or win error code on fail
  success := FileSetDate(FH, FDate);
  SuccessLabel.Caption := IntToStr(success);
  // close the file with the counterpart to FileOpen()
  FileClose(FH);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := '6/15/1997 10:10:10 AM';
end;

end.