Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Verzeichnislisting als Baumansicht in StringListe (https://www.delphipraxis.net/169374-verzeichnislisting-als-baumansicht-stringliste.html)

Luckie 16. Jul 2012 09:38

Verzeichnislisting als Baumansicht in StringListe
 
Ich durchsuche ein Verzeichnis mit FindFirst und schreibe das Ergebnis in eine StringListe:
Delphi-Quellcode:
procedure TDTListDir.FindFiles(RootFolder: AnsiString; Mask: AnsiString = '*.*'; Recurse: Boolean = True);
var
  SR: TSearchRec;
begin
  RootFolder := IncludeTrailingPathDelimiter(RootFolder);

  if Recurse then
    if FindFirst(RootFolder + '*.*', faAnyFile, SR) = 0 then
    try
      repeat
        if SR.Attr and faDirectory = faDirectory then
          if (SR.Name <> '.') and (SR.Name <> '..') then
            FindFiles(RootFolder + SR.Name, Mask, Recurse);
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  if FindFirst(RootFolder + Mask, faAnyFile, SR) = 0 then
  try
    repeat
      if SR.Attr and faDirectory <> faDirectory then
      begin
        sl.Add(RootFolder + SR.Name + Format('(%d, %s)',
        [SR.Size, FormatDateTime('yyyy-mm-dd hh:nn:ss',FileDateToDateTime(SR.Time))]));
      end;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;
Das Ergebnis sieht dann so aus:
Code:
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.dfm.~1~(606, 2012-04-20 22:35:10)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~10~(2593, 2012-04-20 23:03:16)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~1~(2205, 2012-04-20 22:37:30)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~2~(2234, 2012-04-20 22:41:12)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~3~(2608, 2012-04-20 22:49:04)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~4~(2618, 2012-04-20 22:51:00)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~5~(2622, 2012-04-20 22:55:58)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~6~(2622, 2012-04-20 22:57:18)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~7~(2613, 2012-04-20 22:58:18)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~8~(2609, 2012-04-20 22:58:38)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit1.pas.~9~(2609, 2012-04-20 23:02:56)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.dfm.~1~(854, 2012-05-09 17:52:44)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.dfm.~2~(979, 2012-05-09 17:53:10)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.dfm.~3~(1073, 2012-05-09 17:58:12)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.dfm.~4~(1422, 2012-05-09 18:04:12)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~10~(2018, 2012-05-09 18:18:14)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~11~(2013, 2012-05-09 18:19:50)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~2~(873, 2012-05-09 17:56:48)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~3~(873, 2012-05-09 17:57:38)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~4~(1543, 2012-05-09 17:58:28)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~5~(1951, 2012-05-09 18:06:32)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~6~(1948, 2012-05-09 18:08:12)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~7~(2006, 2012-05-09 18:09:32)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~8~(2117, 2012-05-09 18:12:16)
C:\Users\Michael\Documents\Borland Studio-Projekte\__history\Unit2.pas.~9~(2006, 2012-05-09 18:14:48)
C:\Users\Michael\Documents\Borland Studio-Projekte\HTTPDownloadDemo.zip(316321, 2012-05-09 18:26:00)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project1.bdsproj.local(99, 2012-06-18 13:36:28)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project1.identcache(153, 2012-07-05 01:57:12)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.bdsproj(8473, 2012-05-09 18:10:06)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.bdsproj.local(470, 2012-05-09 18:10:06)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.cfg(452, 2012-05-09 18:24:34)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.dpr(188, 2012-05-09 18:10:06)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.exe(640000, 2012-05-09 18:21:38)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.identcache(153, 2012-05-09 18:24:34)
C:\Users\Michael\Documents\Borland Studio-Projekte\Project2.res(4396, 2012-05-09 17:43:34)
C:\Users\Michael\Documents\Borland Studio-Projekte\Unit2.dcu(6676, 2012-05-09 18:21:36)
C:\Users\Michael\Documents\Borland Studio-Projekte\Unit2.dfm(1404, 2012-05-09 18:10:00)
C:\Users\Michael\Documents\Borland Studio-Projekte\Unit2.pas(2060, 2012-05-09 18:21:34)
C:\Users\Michael\Documents\Meine empfangenen Dateien\2012-06-26-00 (Geschnitten).wmv(58629928, 2012-06-30 04:01:04)
C:\Users\Michael\Documents\My Games\Beyond the Sword\Logs\init.log(336, 2012-06-28 09:07:32)
C:\Users\Michael\Documents\My Games\Beyond the Sword\Logs\PythonErr2.log(7556, 2012-06-28 09:07:40)
C:\Users\Michael\Documents\My Games\Beyond the Sword\Logs\resmgr.log(0, 2012-06-28 09:07:32)
C:\Users\Michael\Documents\My Games\Beyond the Sword\Logs\xml.log(477, 2012-06-28 09:07:32)
C:\Users\Michael\Documents\My Games\Beyond the Sword\ScreenShots\FrozenScreen.tga(3686431, 2012-06-28 09:07:44)
C:\Users\Michael\Documents\My Games\Beyond the Sword\CivilizationIV.ini(5591, 2012-06-28 09:07:56)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Logs\init.log(322, 2012-06-28 09:14:16)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Logs\PythonErr2.log(5973, 2012-06-28 09:43:20)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Logs\resmgr.log(0, 2012-06-28 09:14:16)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Logs\xml.log(477, 2012-06-28 09:14:16)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_Initial_n. Chr.-1492.ColonizationSave(49372, 2012-06-28 09:16:06)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_n. Chr.-1564.ColonizationSave(85614, 2012-06-28 09:34:40)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_n. Chr.-1568.ColonizationSave(87469, 2012-06-28 09:36:50)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_n. Chr.-1572.ColonizationSave(87887, 2012-06-28 09:38:08)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_n. Chr.-1576.ColonizationSave(88959, 2012-06-28 09:39:52)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\auto\AutoSave_n. Chr.-1580.ColonizationSave(90802, 2012-06-28 09:41:42)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\single\Michael n. Chr.-1583.ColonizationSave(92585, 2012-06-28 09:43:08)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\Saves\WorldBuilder\WBQuickSave.ColonizationWBSave(179790, 2012-06-28 09:16:06)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\ScreenShots\FrozenScreen.tga(3686431, 2012-06-28 09:42:30)
C:\Users\Michael\Documents\My Games\Sid Meier's Civilization IV Colonization\CivilizationIV.ini(5497, 2012-06-28 09:17:10)
C:\Users\Michael\Documents\My Games\Warlords\Logs\init.log(311, 2012-06-28 07:03:06)
C:\Users\Michael\Documents\My Games\Warlords\Logs\PythonErr2.log(6283, 2012-06-28 07:03:16)
C:\Users\Michael\Documents\My Games\Warlords\Logs\resmgr.log(0, 2012-06-28 07:03:04)
C:\Users\Michael\Documents\My Games\Warlords\Logs\xml.log(471, 2012-06-28 07:03:06)
C:\Users\Michael\Documents\My Games\Warlords\Replays\Michael_n. Chr.-1330_0.CivWarlordsReplay(205617, 2012-06-28 09:04:54)
C:\Users\Michael\Documents\My Games\Warlords\Replays\Minimap.dds(131200, 2012-06-28 09:04:54)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_Initial_v. Chr.-4000.CivWarlordsSave(66705, 2012-06-28 07:07:38)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_n. Chr.-1160.CivWarlordsSave(196248, 2012-06-28 08:53:00)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_n. Chr.-1200.CivWarlordsSave(200851, 2012-06-28 08:57:00)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_n. Chr.-1240.CivWarlordsSave(205031, 2012-06-28 08:59:14)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_n. Chr.-1280.CivWarlordsSave(210175, 2012-06-28 09:02:12)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\auto\AutoSave_n. Chr.-1320.CivWarlordsSave(213261, 2012-06-28 09:03:34)
C:\Users\Michael\Documents\My Games\Warlords\Saves\single\Michael n. Chr.-1330.CivWarlordsSave(214985, 2012-06-28 09:04:40)
C:\Users\Michael\Documents\My Games\Warlords\Saves\WorldBuilder\WBQuickSave.CivWarlordsWBSave(379053, 2012-06-28 07:07:38)
C:\Users\Michael\Documents\My Games\Warlords\CivilizationIV.ini(5780, 2012-06-28 09:05:54)
C:\Users\Michael\Documents\Scanned Documents\desktop.ini(81, 2012-03-13 14:53:20)
C:\Users\Michael\Documents\Default.rdp(0, 2012-03-16 22:23:44)
C:\Users\Michael\Documents\desktop.ini(402, 2012-07-11 13:31:18)
C:\Users\Michael\Documents\TXCUserDictionary.dic(620, 2012-05-01 20:20:50)
Das ist natürlich etwas unübersichtlich. Wie bekomme ich dass jetzt in eine Baumansicht als Text?

Code:
C:\Users
   Michael
      Documents
         Borland Studio-Projekte
            __history
               Unit1.dfm.~1~(606, 2012-04-20 22:35:10)
               Unit1.pas.~10~(2593, 2012-04-20 23:03:16)
         Meine empfangenen Dateien
            2012-06-26-00 (Geschnitten).wmv(58629928, 2012-06-30 04:01:04)
         My Games
            Beyond the Sword
               Logs
                  init.log(336, 2012-06-28 09:07:32)
Mir fehlt da irgendwie gerade die Idee.

Oder hatten wir so was schon mal un dich habe es nicht gefunden?

Das kann entweder sofort passieren oder man kann es auch nachher parsen und die Baumansicht nachträglich erzeugen. Das ist egal.

himitsu 16. Jul 2012 09:41

AW: Verzeichnislisting als Baumansicht in StringListe
 
Du fügst beim Add immer nur den Dateinamen hinzu und vor den Namen noch ein paar Leerzeichen.

Dafür einfach noch einen weiteren Parameter für das Level oder direkt einen string für die Parameter, wo du dann jeweils hochzählst (für Delphi-Referenz durchsuchenDupeString) oder neue Leerzeichen (z.B. je 2-3) einfügst, pro Leven/Ebene.

DeddyH 16. Jul 2012 09:48

AW: Verzeichnislisting als Baumansicht in StringListe
 
Wie wäre es mit einer String-Variablen "Prefix" oder so? Wenn ein Verzeichnis gefunden wird, wird diese um einen Tabulator erweitert, nach Austritt aus der Rekursion wird der letzte wieder gelöscht. Eingetragen wird dann nicht mehr der komplette Pfad, sondern nur Prefix + Name.

[edit] *Gnarf*, das kommt davon, wenn während des Tippens ständig das Telefon läutet. [/edit]

Luckie 16. Jul 2012 09:58

AW: Verzeichnislisting als Baumansicht in StringListe
 
Also für die Dateien habe ich es schon mal hinbekommen:
Delphi-Quellcode:
procedure TDTListDir.FindFiles(RootFolder: AnsiString; Mask: AnsiString = '*.*'; Recurse: Boolean = True);
var
  SR: TSearchRec;
  i: Integer;
  Tab: AnsiString;
begin
  RootFolder := IncludeTrailingPathDelimiter(RootFolder);

  if Recurse then
    if FindFirst(RootFolder + '*.*', faAnyFile, SR) = 0 then
    try
      repeat
        if SR.Attr and faDirectory = faDirectory then
          if (SR.Name <> '.') and (SR.Name <> '..') then
          begin
            FindFiles(RootFolder + SR.Name, Mask, Recurse);
          end;
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
      Inc(FLevel);
    end;
  if FindFirst(RootFolder + Mask, faAnyFile, SR) = 0 then
  try
    for i := 0 to FLevel do
      Tab := Tab + ' ';
    repeat
      if SR.Attr and faDirectory <> faDirectory then
      begin
        sl.Add(Tab + SR.Name + Format('(%d, %s)',
        [SR.Size, FormatDateTime('yyyy-mm-dd hh:nn:ss', FileDateToDateTime(SR.Time))]));
      end;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;
Nur bei den Ordnern hapert es noch. Wie bekomme ich das hin?

joachimd 16. Jul 2012 10:43

AW: Verzeichnislisting als Baumansicht in StringListe
 
Gib eine Level-Variable mit rein:


Delphi-Quellcode:
procedure TDTListDir.FindFiles(RootFolder: AnsiString; Mask: AnsiString = '*.*'; iLevel: integer=0; Recurse: Boolean = True);
...
FindFiles(Folder, Mask, iLevel+1, Recurse);
...
Anhand des Levels kommen dann die Tabs/Leerzeichen.

Luckie 16. Jul 2012 10:55

AW: Verzeichnislisting als Baumansicht in StringListe
 
So passt es fast:
Delphi-Quellcode:
procedure TDTListDir.FindFiles(RootFolder: AnsiString; Level: Integer = 0; Mask: AnsiString = '*.*'; Recurse: Boolean = True);
var
  SR: TSearchRec;
  i: Integer;
  Tab: AnsiString;
  slFolder: TStringList;
begin
  slFolder := TStringList.Create;
  slFolder.Delimiter := '\';
  slFolder.StrictDelimiter := True;

  Inc(Level);
  RootFolder := IncludeTrailingPathDelimiter(RootFolder);

  if Recurse then
  begin
    if FindFirst(RootFolder + '*.*', faAnyFile, SR) = 0 then
    try
      repeat
        if SR.Attr and faDirectory = faDirectory then
          if (SR.Name <> '.') and (SR.Name <> '..') then
          begin
            FindFiles(RootFolder + SR.Name, Level, Mask, Recurse);
          end;
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  end;
  if FindFirst(RootFolder + Mask, faAnyFile, SR) = 0 then
  try
    slFolder.DelimitedText := RootFolder;
    Tab := '';
    for i := 0 to Level do
      Tab := Tab + ' ';
    writeln(Tab + slFolder[Level]);
    repeat
      if SR.Attr and faDirectory <> faDirectory then
      begin
        sl.Add(Tab + SR.Name + Format('(%d, %s)',
        [SR.Size, FormatDateTime('yyyy-mm-dd hh:nn:ss', FileDateToDateTime(SR.Time))]));
      end;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
  slFolder.Free;
end;
Nur habe ich da Ordner mehrfach drin. Was habe ich über sehen?


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:04 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