Like others already said: you still have to count all files manually
(with a FindFirst/FindNext/FindClose-structure).
If you want I got a simple threaded fileparser-code, by assigning an
OnFind-event you can simply increase a count-integer variable:
[code]
Unit FileFinder;
Interface
Uses
Classes, SysUtils;
Type
TFinderFindEvent = Procedure(FileName : String; Var Abort : Boolean) Of
Object;
TFileFinder = class(TThread)
Private
fAbort : Boolean;
fCur : String;
fPath : String;
fExt : String;
fRec : Boolean;
fOnFind : TFinderFindEvent;
Protected
Procedure Execute; Override;
Procedure UpdateFind;
Public
Constructor Create(AFilePath : String;
AExtension : String;
ARecursive : Boolean;
AOnFind : TFinderFindEvent);
Procedure Abort;
End;
Implementation
Procedure TFileFinder.Execute;
Procedure ProcessRec(Rec : TSearchRec);
Begin
fCur := fPath + Rec.Name;
If (Not fAbort) Then
If (Rec.Attr And faDirectory > 0) And
(Rec.Name <> '.') And
(Rec.Name <> '..') And
(fRec) Then
TFileFinder.Create(fCur, fExt, fRec, fOnFind)
Else
If (fExt = '') Or
(LowerCase(ExtractFileExt(Rec.Name)) = fExt) Then
Synchronize(UpdateFind);
End;
Var
R : TSearchRec;
Begin
fAbort := False;
fCur := '';
fPath := IncludeTrailingPathDelimiter(fPath);
If (FindFirst(fPath + '*.*', faAnyFile, R) = 0) Then
Try
ProcessRec(R);
While (Not fAbort) And
(FindNext(R) = 0) Do
ProcessRec(R);
Finally
FindClose(R);
End;
End;
Constructor TFileFinder.Create(AFilePath : String;
AExtension : String;
ARecursive : Boolean;
AOnFind : TFinderFindEvent);
Begin
Inherited Create(True);
FreeOnTerminate := True;
If Assigned(AOnFind) Then
Begin
fPath := AFilePath;
fExt := LowerCase(AExtension);
fRec := ARecursive;
fOnFind := AOnFind;
Resume;
End
Else
Free;
End;
Procedure TFileFinder.UpdateFind;
Begin
If Assigned(fOnFind) Then
fOnFind(fCur, fAbort)
Else
fAbort := True;
End;
Procedure TFileFinder.Abort;
Begin
fAbort := True;
End;
End.
[/code]
Greetz,
Peter.
-----Oorspronkelijk bericht-----
Van: [email protected] [mailto:[EMAIL PROTECTED]
mauro russo
Verzonden: maandag 12 mei 2008 10:49
Aan: delphi-en
Onderwerp: [delphi-en] file counting
Dear all,
is it possible to obtain the number of files in a directory (no
sub-directories)
without to enumerate all files, for example as FindFirst and FindNext?
Thanks in advance,
Mauro Russo.