Thema: Delphi FindFirst, FindNext

Einzelnen Beitrag anzeigen

Benutzerbild von nailor
nailor

Registriert seit: 12. Dez 2002
Ort: Karlsruhe
1.989 Beiträge
 
#10
  Alt 8. Jan 2003, 12:15
Was mach ich jetzt schon wieder falsch?

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var
  a: TSearchRec;
begin
ListBox1.Clear;
SetCurrentDir('C:\');

FindFirst('*.*', faDirectory , a);
ListBox1.Items.Append(a.Name);
while FindNext(a) = 0 do
  ListBox1.Items.Append(a.name);
FindClose(a);

ListBox1.Items.Append('');
ListBox1.Items.Append(stringofchar('#', 75));
ListBox1.Items.Append('');

if FindFirst('*.*', faAnyFile and not faDirectory, a) = 0 then
  begin
    ListBox1.Items.Append(a.Name);
    while FindNext(a) = 0 do
      ListBox1.Items.Append(a.name);
  end
else
  ListBox1.Items.Append('no files found');
FindClose(a);
end;
Das Obere zeigt alle Ordner und alle Dateien an...

PS: Die Delphi-Hilfe:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);

var
  sr: TSearchRec;
  FileAttrs: Integer;
begin
  StringGrid1.RowCount := 1;
  if CheckBox1.Checked then
    FileAttrs := faReadOnly
  else
    FileAttrs := 0;
  if CheckBox2.Checked then
    FileAttrs := FileAttrs + faHidden;
  if CheckBox3.Checked then
    FileAttrs := FileAttrs + faSysFile;
  if CheckBox4.Checked then
    FileAttrs := FileAttrs + faVolumeID;
  if CheckBox5.Checked then

    FileAttrs := FileAttrs + faDirectory;
  if CheckBox6.Checked then
    FileAttrs := FileAttrs + faArchive;
  if CheckBox7.Checked then

    FileAttrs := FileAttrs + faAnyFile;

  with StringGrid1 do
  begin
    RowCount := 0;

    if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then

    begin
      repeat
        if (sr.Attr and FileAttrs) = sr.Attr then
        begin
        RowCount := RowCount + 1;
        Cells[1,RowCount-1] := sr.Name;
        Cells[2,RowCount-1] := IntToStr(sr.Size);
        end;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;
end;
arbeitet auch ohne and/and not. Nur zum Testen von gesetzten Werten:

To test for an attribute, combine the value of the Attr field with the attribute constant with the and operator. If the file has that attribute, the result will be greater than 0. For example, if the found file is a hidden file, the following expression will evaluate to True: (SearchRec.Attr and faHidden) <> 0.
Michael N.
http://nailor.devzero.de/code/sharpmath/testing/ --- Tests, Feedback, Anregungen, ... aller Art sehr willkommen!
::: don't try so hard - it'll happen for a reason :::
  Mit Zitat antworten Zitat