SetLength löscht Array Inhalt
Ich suche mit FindFirstFile Dateien auf der Festplatte und schreibe sie in ein dynamisches Array. Da es nicht sehr ressourcen schonend ist jedes mal das Array um eins zu vergrößern, habe ich mir folgendes gemacht:
Delphi-Quellcode:
Files ist global, sobald aber der erste rekursive Aufruf kommt ist das Array aber wieder leer:
Filename := RootFolder + string(wfd.cFileName);
if length(Files) = cnt then SetLength(Files, length(Files)+100); Files[cnt] := Filename; Inc(cnt);
Delphi-Quellcode:
Und das verstehe ich nicht ganz. Ich wollte die Routine in einem nonVCL Programm nutzen, also ohne die Unit Classes auskommen.
procedure FindAllFiles(RootFolder: string; Mask: string =
'*.*'; Recurse: Boolean = True); var hFindFile : THandle; wfd : TWin32FindData; Filename : string; cnt : Integer; begin cnt := 0; if RootFolder[length(RootFolder)] <> '\' then RootFolder := RootFolder + '\'; ZeroMemory(@wfd, sizeof(wfd)); wfd.dwFileAttributes := FILE_ATTRIBUTE_NORMAL; if Recurse then begin hFindFile := FindFirstFile(pointer(RootFolder + '\' + '*.*'), wfd); if hFindFile <> 0 then try repeat if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY then if (string(wfd.cFileName) <> '.') and (string(wfd.cFileName) <> '..') then begin FindAllFiles(RootFolder + wfd.cFileName, Mask, Recurse); end; until FindNextFile(hFindFile, wfd) = False; finally FindClose(hFindFile); end; end; hFindFile := FindFirstFile(pointer(RootFolder + '\' + '*.*'), wfd); if hFindFile <> 0 then try repeat if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> FILE_ATTRIBUTE_DIRECTORY then begin Filename := RootFolder + string(wfd.cFileName); if length(Files) = cnt then SetLength(Files, length(Files)+100); Files[cnt] := Filename; Inc(cnt); end; until FindNextFile(hFindFile, wfd) = False; finally FindClose(hFindFile); setlength(Files, cnt); end; end; |
Re: SetLength löscht Array Inhalt
Zitat:
Delphi-Quellcode:
Ich weiss nicht wie length(Files) arbeitet, ich nutzer eigentlich immer High(Files)+1. Vielleicht behebt das ja schon das Problem. Ich hatte mit Setlenght so noch nie das Problem, daß vorhandene Array-Einträge gelöscht wurden.
Files = array of string ;
|
Re: SetLength löscht Array Inhalt
Jupp, ein dynamisches Array of String, entspricht TStringDynArray.
|
Re: SetLength löscht Array Inhalt
Hi,
was möchtest Du denn mit dem zweiten "setlength(Files, cnt);" in dem finally Abschnitt bewirken? Du setzt die länge doch bereits, oder nicht? mfG mirage228 |
Re: SetLength löscht Array Inhalt
müll
|
Re: SetLength löscht Array Inhalt
vielleicht hilft es am anfang ein
Delphi-Quellcode:
einzufügen. ansonsten benutzte ich dynamische arrays genauso, und hab damit keine probleme.
setlength(files, 0);
rantanplan |
Re: SetLength löscht Array Inhalt
Zitat:
|
Re: SetLength löscht Array Inhalt
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:14 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz