Einzelnen Beitrag anzeigen

Benutzerbild von Tommy111
Tommy111

Registriert seit: 2. Nov 2004
Ort: Kirchberg
35 Beiträge
 
Delphi 6 Professional
 
#8

Re: Textdatei, wie Erstellt am und Name der Datei auslesen

  Alt 16. Nov 2004, 21:06
Zunächst eine Funktion, die die drei Datumswerte (Erstellungsdatum, Datum der letzten Änderung und Datum des letzten Zugriffs) zurückgibt. Die Funktion gibt false zurück, wenn sie nicht erfolgreich war.
Delphi-Quellcode:
function GetFileDate(const FileName: string; out Creation, LastAccess,
  LastWrite: TDateTime): Boolean;
var
  hFile: THandle;
  ftCreationUTC, ftLastAccessUTC, ftLastWriteUTC: TFileTime;
  ftCreationLocal, ftLastAccessLocal, ftLastWriteLocal: TFileTime;
  stCreationLocal, stLastAccessLocal, stLastWriteLocal: TSystemTime;
begin
  result:=false;
  hFile := CreateFile(PChar(FileName), GENERIC_READ, 0, nil,
  OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  if (hFile <> INVALID_HANDLE_VALUE) then begin
    try
      //Ermittlung des Dateidatums in UTC (Weltzeit)
      if GetFileTime(hFile, @ftCreationUTC, @ftLastAccessUTC, @ftLastWriteUTC) then begin
        //Umrechnung in Ortszeit
        if FileTimeToLocalFileTime(ftCreationUTC, ftCreationLocal)
        and FileTimeToLocalFileTime(ftLastAccessUTC, ftLastAccessLocal)
        and FileTimeToLocalFileTime(ftLastWriteUTC, ftLastWriteLocal) then begin
          //Umwandlung in Systemdatumformat
          if FileTimeToSystemTime(ftCreationLocal, stCreationLocal)
          and FileTimeToSystemTime(ftLastAccessLocal, stLastAccessLocal)
          and FileTimeToSystemTime(ftLastWriteLocal, stLastWriteLocal) then begin
            //Zuweisung der Rückgabewerte
            Creation := SystemTimeToDateTime(stCreationLocal);
            LastAccess := SystemTimeToDateTime(stLastAccessLocal);
            LastWrite := SystemTimeToDateTime(stLastWriteLocal);
            result:=true;
          end;
        end;
      end;
    finally
      CloseHandle(hFile);
    end;
  end;
end;
Aufrufbeispiel:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var erstellung, zugriff, schreibzugriff: TDateTime;
begin
  GetFileDate('c:\temp\test.bmp', erzeugung, zugriff, schreibzugriff);
  ShowMessage('Erstellung: '+DateTimeToStr(erstellung)+#13#10+'Letzter Zugriff: '+
  DateTimeToStr(zugriff)+#13#10+'Letzter Schreibzugriff: '+DateTimeToStr(schreibzugriff));
end;
In folgender Prozedur wird dargestellt, wie diese drei Daten eines Ordners oder einer Datei auch geändert werden können.
Delphi-Quellcode:
function ChangeFileDate(const path: string; const Creation, LastAccess,
  LastWrite: TDateTime): Boolean;
var
  hFile: THandle;
  ftCreationUTC, ftLastAccessUTC, ftLastWriteUTC: TFileTime;
  ftCreationLocal, ftLastAccessLocal, ftLastWriteLocal: TFileTime;
  stCreationLocal, stLastAccessLocal, stLastWriteLocal: TSystemTime;
begin
  result := false;
  hFile := CreateFile(PChar(path), GENERIC_READ or GENERIC_WRITE, 0, nil,
  OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  if (hFile <> INVALID_HANDLE_VALUE) then
    try
      //Umwandlung von TDateTime in Systemzeitformat
      DateTimeToSystemTime(Creation, stCreationLocal);
      DateTimeToSystemTime(LastAccess, stLastAccessLocal);
      DateTimeToSystemTime(LastWrite, stLastWriteLocal);
      //Umwandlung von Systemzeitformat in lokales Dateizeitformat
      if (SystemTimeToFileTime(stCreationLocal, ftCreationLocal)) and
      (SystemTimeToFileTime(stLastAccessLocal, ftLastAccessLocal)) and
      (SystemTimeToFileTime(stLastWriteLocal, ftLastWriteLocal)) then begin
        //Umwandlung von lokalem Dateizeitformat in Weltzeit
        if (LocalFileTimeToFileTime(ftCreationLocal, ftCreationUTC)) and
        (LocalFileTimeToFileTime(ftLastAccessLocal, ftLastAccessUTC)) and
        (LocalFileTimeToFileTime(ftLastWriteLocal, ftLastWriteUTC)) then begin
          result:=SetFileTime(hFile, @ftCreationUTC, @ftLastAccessUTC, @ftLastWriteUTC);
        end;
      end;
    finally
      CloseHandle(hFile);
    end;
end;
Hoffe ich konnt dir en bißchen weiterhelfen!?
Programmieren macht Spaß..., jedoch nur wenn man das Wirrwar an Befehlen und Funktionen verstanden hat...!!
  Mit Zitat antworten Zitat