Autor Beitrag
DiamondDog
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Mo 10.11.08 13:13 
Hallo Leute,
ich hab da folgenden Code:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Procedure TForm1.FindFiles (aPath, aFindMask: String; aWithSub: Boolean; aResult: tStrings);
Var
  FindRec: tSearchRec;
Begin
  If (aPath = '') or (aFindMask = '') or Not Assigned (aResult) Then
    Exit;

  If aPath[Length (aPath)] <> '\' Then
    aPath := aPath + '\';

  If FindFirst (aPath + aFindMask, faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        aResult.Add (aPath + FindRec.Name);
    Until FindNext (FindRec) <> 0;

  FindClose (FindRec);

  If Not aWithSub Then
    Exit;

  If FindFirst (aPath + '*.*', faAnyFile, FindRec) = 0 Then
    Repeat
      If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
        If Boolean (FindRec.Attr and faDirectory) Then
          FindFiles (aPath + FindRec.Name, aFindMask, aWithSub, aResult);
    Until FindNext (FindRec) <> 0;

   FindClose (FindRec);
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Clear;
  FindFiles ('C:\', '*.ini', True, ListBox1.Items); 
end;
was muß ich ändern das er mir nur die gefundenen ini´s anzeigt und wenn vorhanden davor den entsprechenden Unterordner?
Beispiel:
ausblenden Quelltext
1:
2:
test.ini
unterordnern/test2.ini

Danke schon mal fürs Helfen.

Mfg Dog
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 10.11.08 13:39 
Wenn ich das richtig sehe, sollten die Ordner eigentlich mit übernommen werden. Und für Inis musst Du die Findmask entsprechend setzen ('*.ini').

[edit] :oops:, hatte den Aufruf völlig übersehen [/edit]
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Mo 10.11.08 14:01 
Die Ordner werden ja auch mit übernommen, nur ich bekomme immer den ganzen Pfad ausgegeben wo ich suche. Das heist wenn ich in C:\Programme zB. suche dann steht in der Liste:
C:\Programme\test.ini
C:\Programme\Unterorndern\test2.ini
und er soll aber nur ausspucken:
test.ini
Unterorndern\test2.ini
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 10.11.08 14:06 
Dann lösch den Hauptordner danach am besten in einer Schleife aus den Einträgen, denn durch den rekursiven Aufruf kennst du ja nicht den ursprünglichen Hauptordner (du könntest ihn höchstens unverändert immer mit übergeben, aber das würde auf das gleiche hinauslaufen).
DiamondDog Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 460



BeitragVerfasst: Mo 10.11.08 14:17 
Hab garnicht an diese Möglichkeit gedacht.
Lösung:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
  ListBox1.Clear;
  FindFiles ('C:\Programme', '*.ini', True, ListBox1.Items);
  for i:=0 to Listbox1.Count-1 do
    begin
      ListBox1.Items.Strings[i] := StringReplace(ListBox1.Items.Strings[i], 'C:\Programme', '', [ rfReplaceAll ]);
    end;
end;
Besten Dank.
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 10.11.08 15:11 
Oder FindFiles etwas umändern:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
Procedure TForm1.FindFiles(aPath: string;
                           const aFindMask: String;
                           const aResult: tStrings;
                           const DefPrefix: string = '';
                           aWithSub: Boolean = true);
Var
  FindRec: tSearchRec;
Begin
  If (aPath = '') or (aFindMask = '') or Not Assigned (aResult) Then
    Exit;

  aPath := IncludeTrailingPathDelimiter(aPath);

  If FindFirst (aPath + aFindMask, faAnyFile, FindRec) = 0 Then
    begin
      Repeat
        If (FindRec.Name <> '.') and (FindRec.Name <> '..') Then
          aResult.Add(DefPrefix + FindRec.Name);
      Until FindNext(FindRec) <> 0;
      SysUtils.FindClose(FindRec);
    end;

  If aWithSub and (FindFirst(aPath + '*.*', faDirectory, FindRec) = 0) Then
    begin
      Repeat
        If (FindRec.Name <> '.') and (FindRec.Name <> '..') and
          ((FindRec.Attr and faDirectory) = faDirectory) Then
            FindFiles (aPath + FindRec.Name, aFindMask, aResult, IncludeTrailingPathDelimiter(DefPrefix + FindRec.Name), aWithSub);
      Until FindNext(FindRec) <> 0;
      SysUtils.FindClose(FindRec);
   end;
End;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 10.11.08 15:45 
user profile iconDiamondDog hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
  FindFiles ('C:\Programme', '*.ini', True, ListBox1.Items);					
Wobei dir hoffentlich klar ist, dass das nur bei dir gerade funktioniert, das Programme-Verzeichnis kann ganz anders heißen, Program Files zum Beispiel...

Finden tust du es mit www.delphi-library.d...ner+finden_9428.html und der Konstante CSIDL_PROGRAM_FILES.