Autor Beitrag
Hellcode
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19

WinXP SP2
D7 Ent
BeitragVerfasst: Sa 08.10.05 14:11 
Hallo,

Ich hoffe ich poste im richtigen Forum. Wenn nicht, einfach wegschieben. Also ich habe vor ein Programm zu schreiben, mit welchem man rekursiv nach Dateien suchen kann und zwar inkulsive Dateimaske, ect. Soweit kein Problem. Ich habe aber nur eine Prozedur gefunden mit der rekursiv gesucht werden kann, aber leider bekomme ich die Größe der gesamten gefundenen Dateien nicht heraus. Er gibt zwar etwas wieder. Aber der Wert ist definitiv zu hoch. Die Angabe müsste ja in Bytes sein..

Das ist die ursprüngliche Prozedur zum suchen:

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:
procedure GetFilesInDirectory(Directory: Stringconst Mask: String;
                              List: TStrings;
                              WithSubDirs, ClearList: Boolean);

procedure ScanDir(const Directory: String);
var
  SR: TSearchRec;
begin
  if FindFirst(Directory + Mask, faAnyFile - faDirectory, SR) = 0 then try
    repeat
      List.Add(Directory + SR.Name)
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;

  if WithSubDirs then begin
    if FindFirst(Directory + '*.*', faAnyFile, SR) = 0 then try
      repeat
        if ((SR.attr and faDirectory) = faDirectory) and
           (SR.Name <> '.'and (SR.Name <> '..'then
          ScanDir(Directory + SR.Name + '\');
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  end;
end;

begin
  List.BeginUpdate;
  try
    if ClearList then
      List.Clear;
    if Directory = '' then Exit;
    if Directory[Length(Directory)] <> '\' then
      Directory := Directory + '\';
    ScanDir(Directory);
  finally
    List.EndUpdate;
  end;
end;


Ich habe schon versucht als Rückgabewert die Größe der gefundenen Dateien zu integrieren, aber so hat es nicht funktioniert. Wo ist der Fehler? Bin gerade ein wenig zerstreut. Wahrscheinlich ist es ganz einfach..

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:
function GetFilesInDirectory(Directory: Stringconst Mask: String;
                              List: TStrings;
                              WithSubDirs, ClearList: Boolean):int64;

function ScanDir(const Directory: String):int64;
var
  SR: TSearchRec;
  Groesse:int64;
begin
  if FindFirst(Directory + Mask, faAnyFile - faDirectory, SR) = 0 then try
    repeat
      List.Add(Directory + SR.Name);
      if ((SR.Attr and faDirectory)>0and (SR.Name<>'.'and (SR.Name<>'..'then
        Groesse:=Groesse+SR.Size;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;

  if WithSubDirs then begin
    if FindFirst(Directory + '*.*', faAnyFile, SR) = 0 then try
      repeat
        if ((SR.attr and faDirectory) = faDirectory) and
           (SR.Name <> '.'and (SR.Name <> '..'then
          ScanDir(Directory + SR.Name + '\');
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
  end;
Result := Groesse;
end;

var
Groesse :int64;
begin
  List.BeginUpdate;
  try
    if ClearList then
      List.Clear;
    if Directory = '' then Exit;
    if Directory[Length(Directory)] <> '\' then
      Directory := Directory + '\';
    Groesse := ScanDir(Directory);
  finally
    List.EndUpdate;
  end;
  Result := Groesse;
end;


So möchte ich die Größe erfassen und gleichzeitig die Dateien in einer Listbox haben (Das mit der Listbox funktioniert ja wie gesagt auch):

ausblenden Delphi-Quelltext
1:
2:
label1.caption := inttostr(GetFilesInDirectory('C:\',
'*.exe',ListBox1.Items,false,false));


Auf C:\ (die Option "Unterverzeichnisse miteinbeziehen" ist ja ausgeschaltet) liegt genau eine EXE-Datei. Sie wird in der ListBox angezeigt. Sie ist ca. 222 kb groß. Nach dem Aufruf hat Label1 aber einen Wert von "5537201249105428"..

Kann jemand helfen? Danke im vorraus!!

Gruß,
HellCode
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 08.10.05 14:37 
Ich hab keine 10 Sekunden auf deinen Source gucken müssen, um den Fehler zu beheben.

Schau mal in dein Compiler-Fenster. Falls dort nix steht, schalt einfach mal die Compiler-Warnungen an, da müsste Dir ein Eintrag "Variable Groesse wahrscheinlich nicht initialisiert" auffallen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Hellcode Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19

WinXP SP2
D7 Ent
BeitragVerfasst: Sa 08.10.05 14:47 
ja hast recht. Aber jetzt hab ich eine Zeile über dem Funktionsaufruf "GetFilesInDirectory" Groesse auf 0 gesetzt, dann kommt auf einmal auch nur 0 raus. Ich glaube da ist doch noch etwas mehr faul in meinem code...
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 08.10.05 14:55 
In Zeile 25 weist Du die Größe des Unterverzeichnisses nicht zu.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Hellcode Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19

WinXP SP2
D7 Ent
BeitragVerfasst: Sa 08.10.05 15:40 
ausblenden Delphi-Quelltext
1:
          Groesse := Groesse + ScanDir(Directory + SR.Name + '\');					


so? passiert nix. Oder hab ich das falsch verstanden?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 08.10.05 15:47 
Zeile 25 stimmt so.

Wäre dann noch Zeile 13, wo
ausblenden Delphi-Quelltext
 
13:
{ ... }
      if ((SR.Attr and faDirectory)>0) and (SR.Name<>'.'and (SR.Name<>'..'then

in
ausblenden Delphi-Quelltext
 
13:
{ ... }
      if ((SR.Attr and faDirectory)=0and (SR.Name<>'.'and (SR.Name<>'..'then

geändert werden müsste. Danach sollte es funzen (Hoff ich mal).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Hellcode Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 19

WinXP SP2
D7 Ent
BeitragVerfasst: Sa 08.10.05 15:51 
Super ich dank dir!