Einzelnen Beitrag anzeigen

synex

Registriert seit: 14. Apr 2003
147 Beiträge
 
Delphi 6 Personal
 
#6

Re: Datei öffnen, speichern[Prob gelöst]

  Alt 28. Okt 2003, 19:13
Diese beiden Prozeduren hab ich mir hier her geklaut
Delphi-Quellcode:
//Dateierstellung etc ändern
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;

//datei datum sachen auslesen
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;
Und so hab ich das in meinem Programm umgesetzt: (in der Listbox stehen die dateinamen mit Pfad in der endgültigen Reihenfolge)

Delphi-Quellcode:
procedure TForm1.Button15Click(Sender: TObject);
var i:integer;
  datum,erstellt,geaendert,access:tdatetime;
begin
datum:=now-(listbox1.Count-i); //der Einfachheit halber ist der Datumsunterschied genau 1 Tag
for i:= 0 to listbox1.Count-1 do
  begin
  getfiledate(listbox3.Items.Strings[i],erstellt,access,geaendert); //Daten auslesen
  changefiledate(listbox3.Items.Strings[i],datum,datum,datum); //und neue Daten setzen, Erstellungsdatum, LastAccess und LastWrite wird gleichgesetzt
  end;
end;
Und das wars auch schon.

MfG synex
  Mit Zitat antworten Zitat