source: trunk/FindFile.pas

Last change on this file was 1, checked in by george, 15 years ago
  • New: Imported base application with sample docked windows.
File size: 3.3 KB
Line 
1{TFindFile
2
3Article:
4http://delphi.about.com/library/weekly/aa052300a.htm
5
6Tired of using FindFirst, Next and Close?
7Come see how to encapsulate all those functions
8in a single "find-files-recursively" component.
9It's easy to use, free and with code.
10
11
12********************************************
13Zarko Gajic
14About.com Guide to Delphi Programming
15http://delphi.about.com
16email: delphi.guide@about.com
17********************************************
18
19}
20
21unit FindFile;
22
23interface
24
25uses
26 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, FileCtrl;
27
28type
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
55procedure Register;
56
57implementation
58
59procedure Register;
60begin
61 RegisterComponents('Samples', [TFindFile]);
62end;
63
64constructor TFindFile.Create(AOwner: TComponent);
65begin
66 inherited Create(AOwner);
67 Path := IncludeTrailingBackslash(GetCurrentDir);
68 FileMask := '*.*';
69 FileAttr := [ffaAnyFile];
70 s := TStringList.Create;
71end;
72
73destructor TFindFile.Destroy;
74begin
75 s.Free;
76 inherited Destroy;
77end;
78
79procedure TFindFile.SetPath(Value: string);
80begin
81 if fPath <> Value then
82 begin
83 if Value <> '' then
84 if DirectoryExists(Value) then
85 fPath := IncludeTrailingBackslash(Value);
86 end;
87end;
88
89function TFindFile.SearchForFiles: TStringList;
90begin
91 s.Clear;
92 try
93 FileSearch(Path);
94 finally
95 Result := s;
96 end;
97end;
98
99procedure TFindFile.FileSearch(const InPath : string);
100var Rec : TSearchRec;
101 Attr : integer;
102begin
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;
134end;
135
136end.
137
Note: See TracBrowser for help on using the repository browser.