-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopy of FindFile.pas
169 lines (149 loc) · 3.78 KB
/
Copy of FindFile.pas
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
unit FindFile;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TFindFile = class
private
{ Private declarations }
fScanSubDir : Boolean;
fDirs : TStringList;
FSearching : Boolean;
FFilesList : TStringList;
private
procedure AddFileToFileList(theFile: String);
procedure ScanDir(Dir: String);
public
{ Public declarations }
property Directories : TStringList read fDirs write fDirs;
property ScanSubDir : Boolean read fScanSubDir write fScanSubDir;
property Searching : Boolean read FSearching;
procedure AddDir(Const DirName : String);
function FindOneFile(Const Path:String; Const FileName : String; Const SubDir:Boolean): String;
function FindFile(Const AFilename: String):String;
procedure BuildFileList;
constructor Create;
destructor Destroy; override;
published
property FilesList: TStringList read FFilesList;
end;
implementation
uses
JclFileUtils;
constructor TFindFile.Create;
begin
inherited Create;
fDirs := TStringList.Create;
fScanSubDir := True;
FSearching := False;
FFilesList := TStringList.Create;
end;
destructor TFindFile.Destroy;
begin
FFilesList.Free;
fDirs.free;
inherited Destroy;
end;
procedure TFindFile.AddDir(const DirName : String);
begin
if fDirs.IndexOf(DirName) = -1 then
fDirs.Add(DirName);
end;
function TFindFile.FindOneFile(Const Path : String; Const FileName : String; Const SubDir:Boolean): String;
var
SR : TSearchRec;
FindResult : Integer;
tmpPath : String;
begin
Result := '';
tmpPath := PathAddSeparator(Path);
if FindFirst(tmpPath + FileName, faAnyFile
{+ faHidden + faSysFile + faReadOnly} , SR) = 0 then
Result := tmpPath + SR.Name;
SysUtils.FindClose(SR);
if SubDir and (Result = '') then
begin
FindResult := FindFirst(tmpPath + '*.*', faDirectory, SR);
While (FindResult = 0) and (Result = '') do
Begin
if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then
Result := FindOneFile(tmpPath + SR.Name, FileName, SubDir);
if Result = '' then
FindResult := FindNext(SR);
end;
SysUtils.FindClose(SR);
end;
end;
Function TFindFile.FindFile(Const AFileName: String) : String;
var
i : Integer;
begin
FSearching := True;
if FFilesList.Count = 0 then
begin
BuildFileList;
end;
Result := '';
i := FFilesList.IndexOfName(AFileName);
if i >= 0 then
begin
Result := FFilesList.ValueFromIndex[i] + AFileName;
if Not FileExists(Result) then
begin
FFilesList.Delete(i);
Result := '';
end;
end;
if Result = '' then
begin
i := 0;
Result := '';
while (i <= fDirs.Count - 1) and (Result = '') do
begin
Result := FindOneFile(fDirs[i], AFileName, fScanSubDir);
if Result = '' then
inc(i);
end;
if Result <> '' then
begin
AddFileToFileList(result);
end;
end;
FSearching := False;
end;
procedure TFindFile.AddFileToFileList(theFile: String);
var
aName, aPath: String;
begin
aName := ExtractFileName(theFile);
aPath := ExtractFilePath(theFile);
FFilesList.Add(aName + '=' + aPath);
end;
procedure TFindFile.BuildFileList;
var
i: Integer;
begin
FilesList.Clear;
for i := 0 to FDirs.Count-1 do
begin
ScanDir(FDirs[i]);
end;
end;
procedure TFindFile.ScanDir(dir: String);
var
sr: TSearchRec;
begin
dir := PathAddSeparator(dir);
if FindFirst(dir + '*.mp3', faAnyFile, SR) = 0 then
repeat
AddFileToFileList(dir + sr.Name);
until FindNext(sr) <> 0;
SysUtils.FindClose(sr);
if FindFirst(dir + '*.*', faDirectory, sr) = 0 then
repeat
if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then
ScanDir(dir + SR.Name);
Until FindNext(sr) <> 0;
SysUtils.FindClose(SR);
end;
end.