-
Notifications
You must be signed in to change notification settings - Fork 1
/
findthread.pas
executable file
·153 lines (117 loc) · 3.19 KB
/
findthread.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
//
// Copyright 2012 Shaun Simpson
// shauns2029@gmail.com
//
unit FindThread;
{$mode objfpc}
interface
uses
Classes, SysUtils, FileUtil, LCLProc, SyncObjs;
type
{ TFindFilesThread }
TFindFilesThread = class(TThread)
private
FSearchPath: string;
FExtension: string;
FComplete: boolean;
FCritical: TCriticalSection;
FFileList, FPathList: TStringList;
procedure FindFiles(FilesList, PathList: TStringList; StartDir, Extension: string);
function GetComplete: boolean;
function GetCount: integer;
public
constructor Create(FileList, PathList: TStringList; SearchPath, Extension: string);
destructor Destroy; override;
procedure Execute; override;
published
property Complete: boolean read GetComplete;
property SearchPath: string read FSearchPath;
property Count: integer read GetCount;
end;
implementation
{ TFindFilesThread }
function TFindFilesThread.GetComplete: boolean;
begin
Result := FComplete;
end;
function TFindFilesThread.GetCount: integer;
begin
FCritical.Enter;
Result := FFileList.Count;
FCritical.Leave;
end;
{ The FileList and PathList are used in combination when producing a file file path.
This is done to save memory when storing files.}
constructor TFindFilesThread.Create(FileList, PathList: TStringList;
SearchPath, Extension: string);
begin
inherited Create(True);
FSearchPath := SearchPath;
FComplete := False;
FExtension := Extension;
FFileList := FileList;
FPathList := PathList;
FFileList.Clear;
FPathList.Clear;
FCritical := TCriticalSection.Create;
end;
destructor TFindFilesThread.Destroy;
begin
FCritical.Free;
inherited Destroy;
end;
procedure TFindFilesThread.Execute;
begin
FindFiles(FFileList, FPathList, FSearchPath, FExtension);
FFileList.Sort;
FComplete := True;
end;
// Recursive procedure to build a list of files
procedure TFindFilesThread.FindFiles(FilesList, PathList: TStringList;
StartDir, Extension: string);
var
SR: TSearchRec;
DirList: TStringList;
IsFound: Boolean;
i: integer;
PathIndex: integer;
PathIndexStr: string;
begin
if Terminated then Exit;
if StartDir[length(StartDir)] <> '/' then
StartDir := StartDir + '/';
// Build a list of the files in the directory StartDir
PathIndex := PathList.IndexOf(StartDir);
if PathIndex < 0 then
begin
PathIndex := PathList.Count;
PathList.Add(StartDir);
end;
PathIndexStr := IntToStr(PathIndex);
IsFound :=
FindFirst(StartDir+'*' + Extension, faAnyFile-faDirectory, SR) = 0;
while IsFound do
begin
FCritical.Enter;
FilesList.Add(PathIndexStr + ':' + SR.Name);
FCritical.Leave;
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Build a list of subdirectories
DirList := TStringList.Create;
IsFound := FindFirst(StartDir+'*', faAnyFile and faDirectory, SR) = 0;
while IsFound and not Terminated do begin
if ((SR.Attr and faDirectory) <> 0) and
(SR.Name[1] <> '.') then
DirList.Add(StartDir + SR.Name);
IsFound := FindNext(SR) = 0;
Sleep(10);
end;
FindClose(SR);
// Scan the list of subdirectories
for i := 0 to DirList.Count - 1 do
FindFiles(FilesList, PathList, DirList[i], Extension);
DirList.Free;
end;
end.