Med funktionerna FindFirst och FindNext. Nedanstående exempel är från delphi hjälpen.
--------------------------------------------------------
This example uses a label, a button named Search, and a button named Again on a form. When the user clicks the Search button, the first file in the specified path is found, and the name and the number of bytes in the file appear in the label's caption. Each time the user clicks the Again button, the next matching file name and size is displayed in the label:
var
SearchRec: TSearchRec;
procedure TForm1.SearchClick(Sender: TObject);
begin
FindFirst('c:\Program Files\delphi4\bin\*.*', faAnyFile, SearchRec);
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
end;
procedure TForm1.AgainClick(Sender: TObject);
begin
if (FindNext(SearchRec) = 0)
Label1.Caption := SearchRec.Name + ' is ' + IntToStr(SearchRec.Size) + ' bytes in size';
1: Jag förstod inte riktigt logiken i "(SearchRec.Attr and faDirectory) = faDirectory".
Det funkade perfekt, men varför blir det SearchRec.Attr AND faDirectory?
Hur bör det bli om man bara fill ha filerna, och inte katalogerna?
2: Finns det något enkelt sätt att få FindFirst & FindNext att sortera annorlunda (t.ex. efter filändelse)
//Visar Filelist i Listbox1
ListBox1.Clear;
ListBox1.Items := FileList;
FileList.Free;
end;
//Utför själva filsökningen
procedure GetFiles(Path: string; Attr: Integer; var FList: TStringList);
var
SearchRec: TSearchRec;
R: Integer;
begin
FList.Clear;
R := FindFirst(Path, Attr, SearchRec);
while R = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
FList.Add(SearchRec.Name);
R := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
//Sorterar en TStringlist
function FileExtSort(StrList: TStringList; Item1, Item2: integer): integer;
begin
Result := CompareText(ExtractFileExt(StrList[Item1]), ExtractFileExt(StrList[Item2]));
end;
Jag fattar inte hur and tillsammans med integer funkar.
En titt på "logical (bitwise) operators" i hjälpen, hjälpte inte särskilt mycket.
i ditt exempel:
17 AND 16 = 16
hur går det ihop?
om jag t.ex. bara vill ha kataloger, hur anropar jag FindFirst då? (jag tänkte faDirectory - faAnyFile, men det gjorde ingen skilnad)
varför ger FindFirst(dir + '*', faReadOnly + faArchive, SearchRec) samma som FindFirst(dir + '*', 0, SearchRec)?
Använder man and på enskilda bitvärden så gäller följande tabell
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
Bitmöntren för konstanterna ser ut så här som du ser har varje konstant en bit som är på(1) utom faAnyfile.
faReadOnly 0000000000000001
faHidden 0000000000000010
faSysFile 0000000000000100
faVolumeID 0000000000001000
faDirectory 0000000000010000
faArchive 0000000000100000
=
faAnyFile 0000000000111111
I exemplet så gjordes följande om du kollar i tabellen överst så kanske det klarnar.
Attr := faReadOnly + faDirectory;
x := (Attr and faDirectory);
Attr får bitmönster 0000000000010001
x får bitmönster 0000000000010000
Håller med om att findfirst och findnext är lite skum.
I hjälpfilen står det:
"The Attr parameter specifies the special files to include in addition to all normal files"
Altså får du alltid med alla "normala filer" men du kan lägga till kataloger och andra onormala filer.
Om du bara ska ha kataloger så måste du alltid använda (SearchRec.Attr and faDirectory) = faDirectory).
274 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2