Avatar billede mr_ruggerio Nybegynder
27. juni 2002 - 15:43 Der er 1 kommentar

Manipulér Date modified og Date created

Jeg ønsker at ændre de tidspunkter der angiver hvornår en fil er oprettet og ændret (date modified og date created). Jeg benytter mig af nedenstående kode som - hvis den virkede efter hensigten - skulle synkronisere filen xxx.exe's date modified og date created med explorer.exe's,. men den ændrer åbenbart kun date created.

var WinDir:Array[0..Max_Path] of Char;
    ReferenceFile:string;
    handly:integer;
    AT,CT,WT:TFileTime;
begin
    GetWindowsDirectory(WinDir,Max_Path);
    ReferenceFile:=StrPas(WinDir)+'\Explorer.exe';
    handly:=FileOpen(ReferenceFile,fmOpenRead);
    GetFileTime(handly,@CT,@AT,@WT);
    FileClose(handly);
    handly:=FileOpen('xxx.exe',fmOpenWrite);
    SetFileTime(handly,@CT,@AT,@WT);
    FileClose(handly);
end;
Avatar billede borrisholt Novice
27. juni 2002 - 15:47 #1
prøv det her :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, math, ExtCtrls, DBCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label2: TLabel;
    Button2: TButton;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    CheckBox1: TCheckBox;
    Label6: TLabel;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

type
  TFile = class
  private
    FFileName : TFileName;
    FReadOnly : Boolean;
    FDirectory: Boolean;
    FHidden  : Boolean;
    FArchive  : Boolean;
    FSysFile  : Boolean;
    FVolumeID : Boolean;
    Function  GetFileLastAccessTime : TDateTime;
    procedure SetFileLastAccessTime(const Value: TDateTime);
    function  GetFileLastWriteTime: TDateTime;
    procedure SetFileLastWriteTime(const Value: TDateTime);
    function  GetFileCreationTimeTime: TDateTime;
    procedure SetFileCreationTimeTime(const Value: TDateTime);
    function  GetFileAttributes: LongInt;
    procedure SetFileAttributes(const Value: LongInt);
    procedure SetArchive(const Value: Boolean);
    procedure SetDirectory(const Value: Boolean);
    procedure SetHidden(const Value: Boolean);
    procedure SetReadOnly(const Value: Boolean);
    procedure SetSysFile(const Value: Boolean);
    procedure SetVolumeID(const Value: Boolean);
  public
    Constructor Create(const FileName : TFileName);
    Destructor Destroy; override;
    property FileLastAccessTime  : TDateTime  read GetFileLastAccessTime    write SetFileLastAccessTime;
    property FileCreationTimeTime : TDateTime  read GetFileCreationTimeTime  write SetFileCreationTimeTime;
    property FileLastWriteTime    : TDateTime  read GetFileLastWriteTime      write SetFileLastWriteTime;
    property FileAttributes      : LongInt    read GetFileAttributes        write SetFileAttributes;
    property Filename            : TFileName  read FFilename;
    property ReadOnly            : Boolean    read FReadOnly                write SetReadOnly;
    property Hidden              : Boolean    read FHidden write SetHidden;
    property SysFile              : Boolean    read FSysFile write SetSysFile;
    property Directory            : Boolean    read FDirectory write SetDirectory;
    property Archive              : Boolean    read FArchive write SetArchive;
    property VolumeID            : Boolean read FVolumeID write SetVolumeID;
  end;

{ TFile }

constructor TFile.Create(const FileName: TFileName);
begin
  inherited Create;
  FFileName := FileName;
end;

destructor TFile.Destroy;
var
  aFileAttribut : Longint;
begin
  aFileAttribut := 0;

  if ReadOnly then
    aFileAttribut := aFileAttribut or faReadOnly;
  if Hidden then
    aFileAttribut := aFileAttribut or faHidden;
  if SysFile then
    aFileAttribut := aFileAttribut or faSysFile;
  if FVolumeID then
    aFileAttribut := aFileAttribut or faVolumeID;
  if FDirectory then
    aFileAttribut := aFileAttribut or faDirectory;
  if FArchive then
    aFileAttribut := aFileAttribut or faArchive;
  FileAttributes := aFileAttribut;
  Inherited;
end;

function TFile.GetFileAttributes: LongInt;
var
  Win32FindData : TWin32FindData;
  Handle : THandle;
begin
  Result:=-1; // Returns -1 if the functions fails
  Handle := Windows.FindFirstFile(PChar(FFilename), Win32FindData);
  if INVALID_HANDLE_VALUE <> Handle then
  begin
    Result :=0;
    Windows.FindClose( Handle );
    if (Win32FindData.dwFileAttributes and faReadOnly) = faReadOnly then
      Result := Result or faReadOnly;

    if (Win32FindData.dwFileAttributes and faHidden) = faHidden then
      Result := Result or faHidden;

    if (Win32FindData.dwFileAttributes and faSysFile) = faSysFile then
      Result := Result or faSysFile;

    if (Win32FindData.dwFileAttributes and faVolumeID) = faVolumeID then
      Result := Result or faVolumeID;

    if (Win32FindData.dwFileAttributes and faDirectory) = faDirectory then
      Result := Result or faDirectory;

    if (Win32FindData.dwFileAttributes and faArchive) = faArchive then
      Result := Result or faArchive;
  end;
end;

function TFile.GetFileCreationTimeTime: TDateTime;
var
  DosFileTime    : DWORD;
  LocalFileTime  : TFileTime;
  Handle : THandle;
begin
  Result:=-1; // Returns -1 if the functions fails
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  if INVALID_HANDLE_VALUE <> Handle then
  begin
    GetFileTime(Handle, @LocalFileTime,nil,nil);
    FileTimeToLocalFileTime( LocalFileTime, LocalFileTime );
    FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo);
    Result := FileDateToDateTime(DosFileTime);
    FileClose( Handle );
  end;
end;

function TFile.GetFileLastAccessTime : TDateTime;
var
  DosFileTime    : DWORD;
  LocalFileTime  : TFileTime;
  Handle : THandle;
begin
  Result:=-1; // Returns -1 if the functions fails
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  if INVALID_HANDLE_VALUE <> Handle then
  begin
    GetFileTime(Handle, nil, @LocalFileTime,nil);
    FileTimeToLocalFileTime( LocalFileTime, LocalFileTime );
    FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo);
    Result := FileDateToDateTime(DosFileTime);
    FileClose( Handle );
  end;
end;

function TFile.GetFileLastWriteTime: TDateTime;
var
  DosFileTime    : DWORD;
  LocalFileTime  : TFileTime;
  Handle : THandle;
begin
  Result:=-1; // Returns -1 if the functions fails
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  if INVALID_HANDLE_VALUE <> Handle then
  begin
    GetFileTime(Handle, @LocalFileTime,nil,nil);
    FileTimeToLocalFileTime( LocalFileTime, LocalFileTime );
    FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo);
    Result := FileDateToDateTime(DosFileTime);
    FileClose( Handle );
  end;
end;

procedure TFile.SetArchive(const Value: Boolean);
begin
  FArchive := Value;
end;

procedure TFile.SetDirectory(const Value: Boolean);
begin
  FDirectory := Value;
end;

procedure TFile.SetFileAttributes(const Value: LongInt);
begin
  Windows.SetFileAttributes(PChar(FFileName), Value)
end;

procedure TFile.SetFileCreationTimeTime(const Value: TDateTime);
var
  LocalFileTime, FileTime: TFileTime;
  Handle : THandle;
  Age    : Longint;
begin
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  Age:=DateTimeToFileDate(Value);
  DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime);
  LocalFileTimeToFileTime(LocalFileTime, FileTime);
  SetFileTime(Handle,@LocalFileTime,nil, nil);
  FileClose(Handle);
end;

procedure TFile.SetFileLastAccessTime(const Value: TDateTime);
var
  LocalFileTime, FileTime: TFileTime;
  Handle : THandle;
  Age    : Longint;
begin
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  Age:=DateTimeToFileDate(Value);
  DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime);
  LocalFileTimeToFileTime(LocalFileTime, FileTime);
  SetFileTime(Handle,nil, @LocalFileTime,nil);
  FileClose(Handle);
end;

procedure TFile.SetFileLastWriteTime(const Value: TDateTime);
var
  LocalFileTime, FileTime: TFileTime;
  Handle : THandle;
  Age    : Longint;
begin
  Handle:=FileOpen(FFileName,fmOpenReadWrite);
  Age:=DateTimeToFileDate(Value);
  DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime);
  LocalFileTimeToFileTime(LocalFileTime, FileTime);
  SetFileTime(Handle,nil,nil,@LocalFileTime);
  FileClose(Handle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  If not OpenDialog1.Execute then
    Exit;

  with TFile.Create(OpenDialog1.FileName) do
  try
    Label2.Caption := FileName;
    Edit1.Text := DateToStr(FileLastAccessTime);
    Edit2.Text := DateToStr(FileCreationTimeTime);
    Edit3.Text := DateToStr(FileLastWriteTime);

    Edit4.Text := TimeToStr(FileLastAccessTime);
    Edit5.Text := TimeToStr(FileCreationTimeTime);
    Edit6.Text := TimeToStr(FileLastWriteTime);

    CheckBox1.Checked := Bool(GetFileAttributes and faReadOnly);
    CheckBox2.Checked := Bool(GetFileAttributes and faHidden);
    CheckBox3.Checked := Bool(GetFileAttributes and faSysFile);
    CheckBox4.Checked := Bool(GetFileAttributes and faVolumeID);
    CheckBox5.Checked := Bool(GetFileAttributes and faDirectory);
    CheckBox6.Checked := Bool(GetFileAttributes and faArchive);
  finally
    free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  aDate : TDateTime;
begin
  with TFile.Create(OpenDialog1.FileName) do
  try
    aDate := StrToDate(Edit1.Text) + StrToTime(Edit4.Text);
    FileLastAccessTime := aDate;

    aDate := StrToDate(Edit2.Text) + StrToTime(Edit5.Text) - StrToTime('01:00:00');
    FileCreationTimeTime := aDate;

    aDate := StrToDate(Edit3.Text) + StrToTime(Edit6.Text) - StrToTime('01:00:00');
    FileLastWriteTime := aDate;

    ReadOnly  := CheckBox1.Checked;
    Hidden    := CheckBox2.Checked;
    SysFile    := CheckBox4.Checked;
    Directory  := CheckBox5.Checked;
    Archive    := CheckBox6.Checked;
    VolumeID  := CheckBox3.Checked;
  finally
    free;
  end;
end;


procedure TFile.SetHidden(const Value: Boolean);
begin
  FHidden := Value;
end;

procedure TFile.SetReadOnly(const Value: Boolean);
begin
  FReadOnly := Value;
end;

procedure TFile.SetSysFile(const Value: Boolean);
begin
  FSysFile := Value;
end;

procedure TFile.SetVolumeID(const Value: Boolean);
begin
  FVolumeID := Value;
end;

end.




Jens B
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester