Autor Beitrag
DorJo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Mi 04.03.09 02:04 
Hy @ all,
ich hab mal wieder ein Problem :oops:
Und zwar möchte ich gern alle Dateien eines Unterverzeichnisses in einem TListview (ListView3) darstellen.
Listview3:
ausblenden volle Höhe 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:
      
object ListView3: TListView
        Left = 14
        Top = 400
        Width = 595
        Height = 264
        Color = clNone
        Columns = <
          item
            Caption = 'Name'
            Width = 320
          end
          item
            Caption = 'Erw'
            Width = 40
          end
          item
            Caption = 'Gr'#246#223'e'
            Width = 41
          end
          item
            Caption = 'Datum'
            Width = 100
          end
          item
            Caption = 'Attr'
            Width = 70
          end>
        Ctl3D = False
        GridLines = True
        ReadOnly = True
        RowSelect = True
        SmallImages = ImageList2
        TabOrder = 8
        ViewStyle = vsReport
        OnDblClick = ListView3DblClick


Bei FormCreate passiert folgendes:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
uses ShellApi;



procedure TForm1.FormCreate(Sender: TObject);
var
  path: string;
begin
  GetDir(0,path);
  path:=path+'\';
  Verzeichnisbaumsteuern(path);
end;


Verzeichnisbaumsteuern ist so aufgebaut (teilweise von den Schweizern abgeguggt :roll: )
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
procedure TForm1.Verzeichnisbaumsteuern(path: string);
var
  SR: TSearchRec;
  F: TSearchRec;
  size: Int64;
  i,e, g,h: integer;
  hFile: THandle;
  rStructur: TWin32FindData;
  rFileTime: TFileTime;
  dwLastAccess: cardinal;
  date1: TDate;
  aFileName,erw,name,drueber: string;
  Info: TSHFileInfo;
  ListItem:TListItem;
  punkt: boolean;
  
begin
  label17.Caption:=path; //Das erscheint über dem Listview damit man weiß in welchen Verzeichnis man steht
  Listview3.Clear; 
  Listbox6.Clear;  //zum Überprüfen der Einträge

  drueber:=path;
  h:=Length(path);
  if h>3 then        //Überprüfen ob ein übergeordnetes Verzeichnis existiert
  begin
    repeat
      Dec(h);
    until (Path[h] = '\') or (h=1);
    if Path[h]='\' then
      drueber:=Copy(Path,1,h);
  end; 
  if (drueber<>path) then   //wenn ja dann als erstes Element in Listview ein '..'
  begin
        ListItem := Listview3.Items.Add;
        Listitem.ImageIndex:=2;
        ListItem.Caption := '..';
        ListItem.SubItems.Add('');
        ListItem.SubItems.Add('DIR');
        ListItem.SubItems.Add('');

  end;
  if (FindFirst(Path + '*.*', faAnyFile, SR) = 0) then  //!!!hier sollte er alle Dateien finden!!!
  begin
    repeat
      if (SR.Attr <> faDirectory) then
      begin

        Listbox6.Items.Add(SR.Name);
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
  for i:=0 to (Listbox6.Items.Count-1) do
  begin

    size := -1;
    aFileName:=path+Listbox6.Items.strings[i];
    if FindFirst(AFileName, faAnyFile, F) = 0 then
    begin
      try
        size :=  F.FindData.nFileSizeLow or (F.FindData.nFileSizeHigh shl 32);
      finally
        SysUtils.FindClose(F);
      end;
    end;

    date1 := 0;  //datum der letzten Änderung herausfinden
    hFile := Windows.FindFirstFile(pchar(aFileName), rStructur);
    if (INVALID_HANDLE_VALUE<>hFile)
    then begin
      Windows.FindClose(hFile);
      FileTimeToLocalFileTime(rStructur.ftLastAccessTime, rFileTime);
      FileTimeToDosDateTime(rFileTime, LongRec(dwLastAccess).Hi, LongRec(dwLastAccess).Lo);
      date1 := FileDateToDateTime(dwLastAccess);
    end;


     erw:='';  //Dateierweiterung und Dateinamen splitten
     e:=Length(Listbox6.Items.strings[i]);
     while e>-1 do     
     begin      
        if (Listbox6.Items.strings[i][e]<>'.') then        
          erw:=Listbox6.Items.strings[i][e]+erw
        else
        begin
          punkt:=true;
          break;
        end;
        e:=e-1;
     end;
     if (punkt=true) then  //Normale Datei in Listview als Eintrag hinzufügen
     begin
        name:='';
        for g:= 1 to e-1 do
        begin
          name:=name+Listbox6.Items.strings[i][g];        
        end;
        ListItem := Listview3.Items.Add;
        Listitem.ImageIndex:=1;
        ListItem.Caption := name;
        ListItem.SubItems.Add(erw);
        ListItem.SubItems.Add(inttostr(size));
        ListItem.SubItems.Add(datetostr(date1));
        punkt:=false;        
     end
     else  //"Ordner" in listview als Eintrag hinzufügen
     begin
        ListItem := Listview3.Items.Add;
        Listitem.ImageIndex:=0;
        ListItem.Caption := Listbox6.Items.Strings[i];
        ListItem.SubItems.Add('');
        ListItem.SubItems.Add('DIR');
        ListItem.SubItems.Add(datetostr(date1));     
     end;
  end;

  ListView3.AlphaSort;

end;


Mit der FormCreate prozedur (also beim ersten Durchlauf) klappt das erstaunlich gut. Er zeigt sogar versteckte Dateien an. Alles ist supi.
Doppelklickt man nun auf einen Eintrag des Listviews entstehen die Probleme. Hier der Code:
ausblenden 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:
procedure TForm1.ListView3DblClick(Sender: TObject);
var i: integer;
    path, drueber: string;
begin
if (ListView3.SelCount<>-1) then
begin
  if (ListView3.Selected.Caption='..') then
  begin
    path:=Label17.Caption;
    drueber:=path;
    i:=Length(Path);
    if i>3 then  //Übergeordnetes Verzeichnis herausfinden
    begin
      repeat
        Dec(i);
      until (Path[i] = '\') or (i=1);
      if Path[i]='\' then
        drueber:=Copy(Path,1,i);
    end;
    if (drueber<>path) then
    begin
      Verzeichnisbaumsteuern(drueber); //Listview neu einlesen
    end;
  end;
end;
end;


So. Es ist vollkommen eigenartig :( Scheinbar zufällig werden Dateien/Ordner weggelassen... Es ist einfach nur komisch. Und ich finde irgendwie keinen Fehler! Könnt ihr mir helfen? Habt ihr vielleicht die zündende Idee?

Ich danke schon mal im vorraus, weil ich weiß das ich mich auf euch verlassen kann!

MfG

DorJo
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: Mi 04.03.09 02:53 
Gibt es einen Grund warum du nicht einfach die eingebauten Methoden benutzt? ExtractFilePath, ExtractFileExt und wie sie alle heißen? So ist dein Quelltext relativ unübersichtlich, weil durch deine selbstgebauten Stringoperationen das ganze recht umfangreich wird (und zudem vielleicht auch Fehler drin sind).
DorJo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Mi 04.03.09 03:00 
Hmm sowas gibts? :shock: ok... hmm gut... ar ich hab grad noch mal gegoogelt und kam auf die richtige Lösung:
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:
      if FindFirst(path + '*.*', faAnyFile, SR) = 0 then
      try
        repeat
          if SR.Attr and faDirectory = faDirectory then
            // --> ein Verzeichnis wurde gefunden
            //   der Verzeichnisname steht in SR.Name
            //   der vollständige Verzeichnisname (inkl. darüberliegender Pfade) ist
            //       RootFolder + SR.Name
            if (SR.Name <> '.') and (SR.Name <> '..') then
              Listbox6.Items.Add(SR.Name);
        until FindNext(SR) <> 0;
      finally
        FindClose(SR);
      end;
  if FindFirst(path + '*.*', faAnyFile, SR) = 0 then
    try
      repeat
        if SR.Attr and faDirectory <> faDirectory then
        begin
          // --> eine Datei wurde gefunden
          //   der Dateiname steht in SR.Name
          //   der vollständige Dateiname (inkl. Pfadangabe) ist
          //       RootFolder + SR.Name

          // folgende Zeile schreibt den vollständigen Namen in eine Memo Feld des
          // Formulars Form1
          Listbox6.Items.Add(SR.Name);
        end;
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;


Aber danke trotzdem für die schnelle Antwort zu so später Stunde!

MfG

DorJo
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: Mi 04.03.09 03:30 
Ja, da gibts einiges. Zum Beispiel das übergeordnete Verzeichnis bekommst du so:
ausblenden Delphi-Quelltext
1:
2:
  drueber := IncludeTrailingPathDelimiter(ExtractFilePath(
    ExcludeTrailingPathDelimiter(Path)));
In drueber steht danach immer das übergeordnete Verzeichnis inklusive abschließendem Pfadtrennzeichen, und wenn Pfad vorher z.B. 'c:\' war, bleibt es 'c:\'. ;-)
DorJo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Mi 04.03.09 03:58 
Hmm... ja sieht einfacher aus! :D
Aber meine Methode funktioniert und da hat man wenn man beispielsweise auf C:\ steht gar nicht die Möglichkeit noch weiter nach oben zu springen! :)