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;
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);
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;
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
Synes godt om
Ny brugerNybegynder
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.