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
|
//
// Sourcecode from http://www.delphi-library.de/topic_47880.html
//
uses Windows, Messages;
const
FFM_INIT = WM_USER + 1976;
FFM_ONFILEFOUND = WM_USER + 1974; // wParam: not used, lParam: Filename
FFM_ONDIRFOUND = WM_USER + 1975; // wParam: NumFolder, lParam: Directory
var
CntFolders : Cardinal = 0;
NumFolder : Cardinal = 0;
////////////////////////////////////////////////////////////////////////////////
//
// FindAllFilesInit
//
//
procedure FindAllFilesInit;
begin
CntFolders := 0;
NumFolder := 0;
end;
////////////////////////////////////////////////////////////////////////////////
//
// CountFolders
//
//
procedure CountFolders(Handle: THandle; RootFolder: string; Recurse: Boolean = True);
var
hFindFile : THandle;
wfd : TWin32FindData;
begin
SendMessage(Handle, FFM_INIT, 0, 0);
if RootFolder[length(RootFolder)] <> '\' then
RootFolder := RootFolder + '\';
ZeroMemory(@wfd, sizeof(wfd));
wfd.dwFileAttributes := FILE_ATTRIBUTE_NORMAL;
if Recurse then
begin
hFindFile := FindFirstFile(pointer(RootFolder + '*.*'), wfd);
if hFindFile <> 0 then
try
repeat
if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY then
begin
if (string(wfd.cFileName) <> '.') and (string(wfd.cFileName) <> '..') then
begin
CountFolders(Handle, RootFolder + wfd.cFileName, Recurse);
end;
end;
until FindNextFile(hFindFile, wfd) = False;
Inc(CntFolders);
finally
Windows.FindClose(hFindFile);
end;
end;
end;
////////////////////////////////////////////////////////////////////////////////
//
// FindAllFiles
//
procedure FindAllFiles(Handle: THandle; RootFolder: string; Mask: string; Recurse: Boolean = True);
var
hFindFile : THandle;
wfd : TWin32FindData;
begin
if RootFolder[length(RootFolder)] <> '\' then
RootFolder := RootFolder + '\';
ZeroMemory(@wfd, sizeof(wfd));
wfd.dwFileAttributes := FILE_ATTRIBUTE_NORMAL;
if Recurse then
begin
hFindFile := FindFirstFile(pointer(RootFolder + '*.*'), wfd);
if hFindFile <> 0 then
try
repeat
if wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = FILE_ATTRIBUTE_DIRECTORY then
begin
if (string(wfd.cFileName) <> '.') and (string(wfd.cFileName) <> '..') then
begin
FindAllFiles(Handle, RootFolder + wfd.cFileName, Mask, Recurse);
end;
end;
until FindNextFile(hFindFile, wfd) = False;
Inc(NumFolder);
SendMessage(Handle, FFM_ONDIRFOUND, NumFolder, lParam(string(RootFolder)));
finally
Windows.FindClose(hFindFile);
end;
end;
hFindFile := FindFirstFile(pointer(RootFolder + Mask), wfd);
if hFindFile <> INVALID_HANDLE_VALUE then
try
repeat
if (wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> FILE_ATTRIBUTE_DIRECTORY) then
begin
SendMessage(Handle, FFM_ONFILEFOUND, 0, lParam(string(RootFolder + wfd.cFileName)));
end;
until FindNextFile(hFindFile, wfd) = False;
finally
Windows.FindClose(hFindFile);
end;
end;
|