![]() |
Funktionsoptimierung
Hallo,
die folgende Routine prüft, ob die in der Stringliste aSource aufgeführten Dateien in den Verzeichnissen aSourceDir und aDestDir existieren. Falls dem so ist, wird weiterhin geprüft, ob die Datei in aSourceDir neuer ist als die in aDestDir. Mich würde jetzt interessieren, ob es bei dieser Routine noch Möglichkeiten zur Optimierung gibt oder ob alle Möglichkeiten ausgereizt sind.
Delphi-Quellcode:
procedure BuildFileList(const aSourceDir, aDestDir: string; aSource, aFiles:
TStringList; const aMaxFileAge: Integer); cdecl; var dtSourceWrite: TDateTime; dtDestWrite: TDateTime; dtCreate: TDateTime; dtWrite: TDateTime; sSourceFileName: string; sDestFileName: string; bDirectory: Boolean; bFileInTarget: Boolean; nFileAge1, nFileAge2: Integer; bCopyFile: Boolean; sFileName: string; begin aFiles.BeginUpdate; for sFileName in aSource do begin sSourceFileName := IncludeTrailingPathDelimiter(aSourceDir) + sFileName; sDestFileName := IncludeTrailingPathDelimiter(aDestDir) + sFileName; bDirectory := IsDirectory(sSourceFileName); if bDirectory then Continue; if FileExists(sDestFileName) then begin if (aMaxFileAge = 0) then begin dtSourceWrite := FileTimeToDateTime(GetFileLastWrite(sSourceFileName)); dtDestWrite := FileTimeToDateTime(GetFileLastWrite(sDestFileName)); bCopyFile := (dtSourceWrite > dtDestWrite); end else begin dtCreate := FileTimeToDateTime(GetFileCreation(sSourceFileName)); dtWrite := FileTimeToDateTime(GetFileLastWrite(sSourceFileName)); nFileAge1 := DaysBetween(dtCreate, Date); nFileAge2 := DaysBetween(dtWrite, Date); bCopyFile := ((nFileAge1 <= aMaxFileAge) or (nFileAge2 <= aMaxFileAge)); end; if bCopyFile then aFiles.Add(sSourceFileName); end else aFiles.Add(sSourceFileName); end; aFiles.EndUpdate; aFiles.Sort; end; |
Re: Funktionsoptimierung
Du könntest einfach FindFirst oder gar direkt FindFirstFile verwenden, um die Dateien zu prüfen.
Dann müssen FileExists und die Datums-Hol-Funktionen nicht jedesmal selber Dateisystemanfragen loslassen, sondern es reicht eine Anfrage und man hat alle Informationen zusammen in dem Record und kann damit schneller vergleichen. |
Re: Funktionsoptimierung
Das Problem ist, das in aSource der Inhalt des Quellverzeichnisses rekursiv gespeichert ist. Aus dem Grund habe ich mich für die String-Liste entschieden, da ich aSource vorher mittels AdvBuildFileList aus der JCL bestücke und den Quellpfad aus den aSource Einträgen lösche.
|
Re: Funktionsoptimierung
Nee nee, ich meinte: Du kannst mit FindFirst recht einfach alle nötigen Information zu einer Datei erhalten.
In "neueren" WindowsVersionen existieren dafür auch direkt Funktionen, welche alle Informationen einer Datei zusammen liefern, aber dieses funktioniert zumindestens immer. Man erhält so praktisch alles von IsDirectory, FileExists, GetFileCreation und GetFileLastWrite über nur ein einziges FindFirst. Läßt sich vom Code her zwar noch weiter kürzen, aber ich hoffe das stimmt erstmal so:
Delphi-Quellcode:
procedure BuildFileList(aSourceDir, aDestDir: string; aSource, aFiles:
TStringList; aMaxFileAge: Integer); cdecl; var sFileName, sSourceFileName: string; hSearch: THandle; rSourceFindData, rDestFindData: TWIN32FindData; ftCreationTime, ftLastWriteTime: TFileTime; begin // einmal reicht ... muß ja nicht bei jeder Datei einzeln aSourceDir := IncludeTrailingPathDelimiter(aSourceDir); aDestDir := IncludeTrailingPathDelimiter(aDestDir); aFiles.BeginUpdate; try for sFileName in aSource do begin sSourceFileName := aSourceDir + sFileName; hSearch := FindFirstFile(PChar(sSourceFileName), rSourceFindData); Windows.FindClose(hSearch); if (hSearch = INVALID_HANDLE_VALUE) or (rSourceFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0) then Continue; hSearch := FindFirstFile(PChar(aDestDir + sFileName), rDestFindData); Windows.FindClose(hSearch); if hSearch = INVALID_HANDLE_VALUE then begin aFiles.Add(sSourceFileName); Continue; end; if aMaxFileAge = 0 then begin if UInt64(rSourceFindData.ftLastWriteTime) > UInt64(rDestFindData.ftLastWriteTime) then aFiles.Add(sSourceFileName); end else begin FileTimeToLocalFileTime(rSourceFindData.ftCreationTime, ftCreationTime); FileTimeToLocalFileTime(rSourceFindData.ftLastWriteTime, ftLastWriteTime); if (DaysBetween(FileTimeToDateTime(ftCreationTime), Date) <= aMaxFileAge) or (DaysBetween(FileTimeToDateTime(ftLastWriteTime), Date) <= aMaxFileAge) then aFiles.Add(sSourceFileName); end; end; aFiles.Sort; finally aFiles.EndUpdate; end; end;
Delphi-Quellcode:
[edit]
procedure BuildFileList(aSourceDir, aDestDir: string; aSource, aFiles:
TStringList; aMaxFileAge: Integer); cdecl; var sFileName, sSourceFileName: string; hSearch: THandle; rSourceFindData, rDestFindData: TWIN32FindData; ftCreationTime, ftLastWriteTime: TFileTime; begin // einmal reicht ... muß ja nicht bei jeder Datei einzeln aSourceDir := IncludeTrailingPathDelimiter(aSourceDir); aDestDir := IncludeTrailingPathDelimiter(aDestDir); aFiles.BeginUpdate; try for sFileName in aSource do begin sSourceFileName := aSourceDir + sFileName; hSearch := FindFirstFile(PChar(sSourceFileName), rSourceFindData); Windows.FindClose(hSearch); if (hSearch = INVALID_HANDLE_VALUE) or (rSourceFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0) then Continue; hSearch := FindFirstFile(PChar(aDestDir + sFileName), rDestFindData); Windows.FindClose(hSearch); if hSearch = INVALID_HANDLE_VALUE then begin aFiles.Add(sSourceFileName); * end * else * if aMaxFileAge = 0 then begin if UInt64(rSourceFindData.ftLastWriteTime) > UInt64(rDestFindData.ftLastWriteTime) then aFiles.Add(sSourceFileName); end else begin FileTimeToLocalFileTime(rSourceFindData.ftCreationTime, ftCreationTime); FileTimeToLocalFileTime(rSourceFindData.ftLastWriteTime, ftLastWriteTime); if (DaysBetween(FileTimeToDateTime(ftCreationTime), Date) <= aMaxFileAge) or (DaysBetween(FileTimeToDateTime(ftLastWriteTime), Date) <= aMaxFileAge) then aFiles.Add(sSourceFileName); end; end; aFiles.Sort; finally aFiles.EndUpdate; end; end; das mit dem FindClose vergeß ich ständig :oops: |
Re: Funktionsoptimierung
Wenn Du jetzt noch
Delphi-Quellcode:
durch
CloseHandle(hSearch);
Delphi-Quellcode:
ersetzt, dann funktioniert der Code auch noch 8-)
Windows.FindClose(hSearch);
Nichts desto trotz: Danke! :thumb: |
Re: Funktionsoptimierung
immer wieder dieses böse FindClose :wall:
Delphi-Quellcode:
procedure BuildFileList(aSourceDir, aDestDir: string; aSource, aFiles:
TStringList; aMaxFileAge: Integer); cdecl; var sFileName: string; hSource, hDest: THandle; rSourceFindData, rDestFindData: TWIN32FindData; ftCreationTime, ftLastWriteTime, ftLastWriteTimeDest: TFileTime; begin aSourceDir := IncludeTrailingPathDelimiter(aSourceDir); aDestDir := IncludeTrailingPathDelimiter(aDestDir); aFiles.BeginUpdate; try for sFileName in aSource do begin hSource := FindFirstFile(PChar(aSourceDir + sFileName), rSourceFindData); Windows.FindClose(hSource); hDest := FindFirstFile(PChar(aDestDir + sFileName), rDestFindData); Windows.FindClose(hDest); FileTimeToLocalFileTime(rSourceFindData.ftCreationTime, ftCreationTime); FileTimeToLocalFileTime(rSourceFindData.ftLastWriteTime, ftLastWriteTime); FileTimeToLocalFileTime(rDestFindData.ftLastWriteTime, ftLastWriteTimeDest); if (hSource <> INVALID_HANDLE_VALUE) and (rSourceFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = 0) and ((aDestDir = INVALID_HANDLE_VALUE) or ((aMaxFileAge = 0) and (ftLastWriteTime > ftLastWriteTimeDest)) or ((aMaxFileAge <> 0) and ((DaysBetween(FileTimeToDateTime(ftCreationTime), Date) <= aMaxFileAge) or (DaysBetween(FileTimeToDateTime(ftLastWriteTime), Date) <= aMaxFileAge)))) then aFiles.Add(aSourceDir + sFileName); end; aFiles.Sort; finally aFiles.EndUpdate; end; end; |
Re: Funktionsoptimierung
hmm ...
also bei
Delphi-Quellcode:
und
and ((aDestDir = INVALID_HANDLE_VALUE)
Delphi-Quellcode:
hast Du Dich wahrscheinlich vertippt. Böses Copy&Paste :wall:
and (ftLastWriteTime > ftLastWriteTimeDest))
Edit: Ich habe mir aus Deinen Tips folgende Funktion gebaut:
Delphi-Quellcode:
procedure BuildFileList(const aSourceDir, aDestDir: string; aSource, aFiles:
TStringList; const aMaxFileAge: Integer); cdecl; var sFileName, sSourceFileName: string; hSource, hDest: THandle; rSourceFindData, rDestFindData: TWIN32FindData; ftCreationTime, ftLastWriteTime, ftLastWriteTimeDest: TFileTime; sSourceDir: string; sDestDir: string; begin sSourceDir := IncludeTrailingPathDelimiter(aSourceDir); sDestDir := IncludeTrailingPathDelimiter(aDestDir); ZeroMemory(@rSourceFindData, SizeOf(TWIN32FindData)); ZeroMemory(@rDestFindData, SizeOf(TWIN32FindData)); aFiles.BeginUpdate; try for sFileName in aSource do begin sSourceFileName := sSourceDir + sFileName; hSource := FindFirstFile(PChar(sSourceFileName), rSourceFindData); Windows.FindClose(hSource); if (hSource = INVALID_HANDLE_VALUE) or (rSourceFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0) then Continue; hDest := FindFirstFile(PChar(sDestDir + sFileName), rDestFindData); Windows.FindClose(hDest); if (hDest = INVALID_HANDLE_VALUE) then aFiles.Add(sSourceFileName) else begin FileTimeToLocalFileTime(rSourceFindData.ftCreationTime, ftCreationTime); FileTimeToLocalFileTime(rSourceFindData.ftLastWriteTime, ftLastWriteTime); FileTimeToLocalFileTime(rDestFindData.ftLastWriteTime, ftLastWriteTimeDest); if (aMaxFileAge = 0) then begin if (FileTimeToDateTime(ftLastWriteTime) > FileTimeToDateTime(ftLastWriteTimeDest)) then aFiles.Add(sSourceFileName); end else begin if (DaysBetween(FileTimeToDateTime(ftCreationTime), Date) <= aMaxFileAge) or (DaysBetween(FileTimeToDateTime(ftLastWriteTime), Date) <= aMaxFileAge) then aFiles.Add(sSourceFileName); end; end; ZeroMemory(@rSourceFindData, SizeOf(TWIN32FindData)); ZeroMemory(@rDestFindData, SizeOf(TWIN32FindData)); end; aFiles.Sort; finally aFiles.EndUpdate; end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:32 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz