1 | {TFindFile
|
---|
2 |
|
---|
3 | Article:
|
---|
4 | http://delphi.about.com/library/weekly/aa052300a.htm
|
---|
5 |
|
---|
6 | Tired of using FindFirst, Next and Close?
|
---|
7 | Come see how to encapsulate all those functions
|
---|
8 | in a single "find-files-recursively" component.
|
---|
9 | It's easy to use, free and with code.
|
---|
10 |
|
---|
11 |
|
---|
12 | ********************************************
|
---|
13 | Zarko Gajic
|
---|
14 | About.com Guide to Delphi Programming
|
---|
15 | http://delphi.about.com
|
---|
16 | email: delphi.guide@about.com
|
---|
17 | ********************************************
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | unit FindFile;
|
---|
22 |
|
---|
23 | interface
|
---|
24 |
|
---|
25 | uses
|
---|
26 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, FileCtrl;
|
---|
27 |
|
---|
28 | type
|
---|
29 | TFileAttrKind = (ffaReadOnly, ffaHidden, ffaSysFile, ffaVolumeID, ffaDirectory, ffaArchive, ffaAnyFile);
|
---|
30 | TFileAttr = set of TFileAttrKind;
|
---|
31 |
|
---|
32 | TFindFile = class(TComponent)
|
---|
33 | private
|
---|
34 | s : TStringList;
|
---|
35 |
|
---|
36 | fSubFolder : boolean;
|
---|
37 | fAttr: TFileAttr;
|
---|
38 | fPath : string;
|
---|
39 | fFileMask : string;
|
---|
40 |
|
---|
41 | procedure SetPath(Value: string);
|
---|
42 | procedure FileSearch(const inPath : string);
|
---|
43 | public
|
---|
44 | constructor Create(AOwner: TComponent); override;
|
---|
45 | destructor Destroy; override;
|
---|
46 |
|
---|
47 | function SearchForFiles: TStringList;
|
---|
48 | published
|
---|
49 | property FileAttr: TFileAttr read fAttr write fAttr;
|
---|
50 | property InSubFolders : boolean read fSubFolder write fSubFolder;
|
---|
51 | property Path : string read fPath write SetPath;
|
---|
52 | property FileMask : string read fFileMask write fFileMask ;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | procedure Register;
|
---|
56 |
|
---|
57 | implementation
|
---|
58 |
|
---|
59 | procedure Register;
|
---|
60 | begin
|
---|
61 | RegisterComponents('Samples', [TFindFile]);
|
---|
62 | end;
|
---|
63 |
|
---|
64 | constructor TFindFile.Create(AOwner: TComponent);
|
---|
65 | begin
|
---|
66 | inherited Create(AOwner);
|
---|
67 | Path := IncludeTrailingBackslash(GetCurrentDir);
|
---|
68 | FileMask := '*.*';
|
---|
69 | FileAttr := [ffaAnyFile];
|
---|
70 | s := TStringList.Create;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | destructor TFindFile.Destroy;
|
---|
74 | begin
|
---|
75 | s.Free;
|
---|
76 | inherited Destroy;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure TFindFile.SetPath(Value: string);
|
---|
80 | begin
|
---|
81 | if fPath <> Value then
|
---|
82 | begin
|
---|
83 | if Value <> '' then
|
---|
84 | if DirectoryExists(Value) then
|
---|
85 | fPath := IncludeTrailingBackslash(Value);
|
---|
86 | end;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function TFindFile.SearchForFiles: TStringList;
|
---|
90 | begin
|
---|
91 | s.Clear;
|
---|
92 | try
|
---|
93 | FileSearch(Path);
|
---|
94 | finally
|
---|
95 | Result := s;
|
---|
96 | end;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | procedure TFindFile.FileSearch(const InPath : string);
|
---|
100 | var Rec : TSearchRec;
|
---|
101 | Attr : integer;
|
---|
102 | begin
|
---|
103 | Attr := 0;
|
---|
104 | if ffaReadOnly in FileAttr then Attr := Attr + faReadOnly;
|
---|
105 | if ffaHidden in FileAttr then Attr := Attr + faHidden;
|
---|
106 | if ffaSysFile in FileAttr then Attr := Attr + faSysFile;
|
---|
107 | if ffaVolumeID in FileAttr then Attr := Attr + faVolumeID;
|
---|
108 | if ffaDirectory in FileAttr then Attr := Attr + faDirectory;
|
---|
109 | if ffaArchive in FileAttr then Attr := Attr + faArchive;
|
---|
110 | if ffaAnyFile in FileAttr then Attr := Attr + faAnyFile;
|
---|
111 |
|
---|
112 | if SysUtils.FindFirst(inPath + FileMask, Attr, Rec) = 0 then
|
---|
113 | try
|
---|
114 | repeat
|
---|
115 | s.Add(inPath + Rec.Name);
|
---|
116 | until SysUtils.FindNext(Rec) <> 0;
|
---|
117 | finally
|
---|
118 | SysUtils.FindClose(Rec);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | If not InSubFolders then Exit;
|
---|
122 |
|
---|
123 | if SysUtils.FindFirst(inPath + '*.*', faDirectory, Rec) = 0 then
|
---|
124 | try
|
---|
125 | repeat
|
---|
126 | if ((Rec.Attr and faDirectory) > 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then
|
---|
127 | begin
|
---|
128 | FileSearch(IncludeTrailingBackslash(inPath + Rec.Name));
|
---|
129 | end;
|
---|
130 | until SysUtils.FindNext(Rec) <> 0;
|
---|
131 | finally
|
---|
132 | SysUtils.FindClose(Rec);
|
---|
133 | end;
|
---|
134 | end;
|
---|
135 |
|
---|
136 | end.
|
---|
137 |
|
---|