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 UFindFile;
|
---|
22 |
|
---|
23 | interface
|
---|
24 |
|
---|
25 | uses
|
---|
26 | SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
|
---|
27 |
|
---|
28 | type
|
---|
29 | EDirNotFound = class(Exception);
|
---|
30 |
|
---|
31 | TFileAttrKind = (ffaReadOnly, ffaHidden, ffaSysFile, ffaVolumeID, ffaDirectory, ffaArchive, ffaAnyFile);
|
---|
32 | TFileAttrib = set of TFileAttrKind;
|
---|
33 |
|
---|
34 | TFindFile = class(TComponent)
|
---|
35 | private
|
---|
36 | s : TStringList;
|
---|
37 | fSubFolder : boolean;
|
---|
38 | fAttr: TFileAttrib;
|
---|
39 | fPath : string;
|
---|
40 | fFileMask : string;
|
---|
41 | procedure SetPath(Value: string);
|
---|
42 | procedure FileSearch(const inPath : string);
|
---|
43 | public
|
---|
44 | constructor Create(AOwner: TComponent); override;
|
---|
45 | destructor Destroy; override;
|
---|
46 | function SearchForFiles: TStringList;
|
---|
47 | published
|
---|
48 | property FileAttr: TFileAttrib read fAttr write fAttr;
|
---|
49 | property InSubFolders : boolean read fSubFolder write fSubFolder;
|
---|
50 | property Path : string read fPath write SetPath;
|
---|
51 | property FileMask : string read fFileMask write fFileMask ;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | const
|
---|
55 | {$IFDEF WINDOWS}
|
---|
56 | FilterAll = '*.*';
|
---|
57 | {$ENDIF}
|
---|
58 | {$IFDEF UNIX}
|
---|
59 | FilterAll = '*';
|
---|
60 | {$ENDIF}
|
---|
61 |
|
---|
62 | procedure Register;
|
---|
63 |
|
---|
64 |
|
---|
65 | implementation
|
---|
66 |
|
---|
67 | resourcestring
|
---|
68 | SDirNotFound = 'Directory not found';
|
---|
69 |
|
---|
70 | procedure Register;
|
---|
71 | begin
|
---|
72 | RegisterComponents('Common', [TFindFile]);
|
---|
73 | end;
|
---|
74 |
|
---|
75 | constructor TFindFile.Create(AOwner: TComponent);
|
---|
76 | begin
|
---|
77 | inherited Create(AOwner);
|
---|
78 | Path := IncludeTrailingBackslash(UTF8Encode(GetCurrentDir));
|
---|
79 | FileMask := FilterAll;
|
---|
80 | FileAttr := [ffaAnyFile];
|
---|
81 | s := TStringList.Create;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | destructor TFindFile.Destroy;
|
---|
85 | begin
|
---|
86 | s.Free;
|
---|
87 | inherited Destroy;
|
---|
88 | end;
|
---|
89 |
|
---|
90 | procedure TFindFile.SetPath(Value: string);
|
---|
91 | begin
|
---|
92 | if fPath <> Value then
|
---|
93 | begin
|
---|
94 | if Value <> '' then
|
---|
95 | if DirectoryExists(UTF8Decode(Value)) then
|
---|
96 | fPath := IncludeTrailingBackslash(Value)
|
---|
97 | else raise EDirNotFound.Create(SDirNotFound);
|
---|
98 | end;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | function TFindFile.SearchForFiles: TStringList;
|
---|
102 | begin
|
---|
103 | s.Clear;
|
---|
104 | try
|
---|
105 | FileSearch(Path);
|
---|
106 | finally
|
---|
107 | Result := s;
|
---|
108 | end;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TFindFile.FileSearch(const InPath : string);
|
---|
112 | var Rec : TSearchRec;
|
---|
113 | Attr : integer;
|
---|
114 | begin
|
---|
115 | Attr := 0;
|
---|
116 | if ffaReadOnly in FileAttr then Attr := Attr + faReadOnly;
|
---|
117 | if ffaHidden in FileAttr then Attr := Attr + 2; //faHidden; use constant to avoid platform warning
|
---|
118 | if ffaSysFile in FileAttr then Attr := Attr + 4; //faSysFile; use constant to avoid platform warning
|
---|
119 | // Deprecated: if ffaVolumeID in FileAttr then Attr := Attr + faVolumeID;
|
---|
120 | if ffaDirectory in FileAttr then Attr := Attr + faDirectory;
|
---|
121 | if ffaArchive in FileAttr then Attr := Attr + faArchive;
|
---|
122 | if ffaAnyFile in FileAttr then Attr := Attr + faAnyFile;
|
---|
123 |
|
---|
124 | if SysUtils.FindFirst(inPath + FileMask, Attr, Rec) = 0 then
|
---|
125 | try
|
---|
126 | repeat
|
---|
127 | s.Add(inPath + Rec.Name);
|
---|
128 | until SysUtils.FindNext(Rec) <> 0;
|
---|
129 | finally
|
---|
130 | SysUtils.FindClose(Rec);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | If not InSubFolders then Exit;
|
---|
134 |
|
---|
135 | if SysUtils.FindFirst(inPath + FilterAll, faDirectory, Rec) = 0 then
|
---|
136 | try
|
---|
137 | repeat
|
---|
138 | if ((Rec.Attr and faDirectory) > 0) and (Rec.Name <> '.')
|
---|
139 | and (Rec.Name <> '..') then
|
---|
140 | FileSearch(IncludeTrailingBackslash(inPath + Rec.Name));
|
---|
141 | until SysUtils.FindNext(Rec) <> 0;
|
---|
142 | finally
|
---|
143 | SysUtils.FindClose(Rec);
|
---|
144 | end;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | end.
|
---|
148 |
|
---|