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
| unit crFilesUtils;
{ unit crFilesUtils
par Cirec
Utilisation :
Procedure FindFiles(
Directory: String; --> In : indique le répertoire de recherche
FilesMask: String; --> In : indique le type de fichier recherché (*?.*)
aList: TStrings; --> Out: TSringList contenant les noms de fichiers trouvés
SubFolders: Boolean--> pour une recherche dans les sous répertoires à True par défaut
FullPath: Boolean --> renvoie le chemin + le nom de fichier ou le nom de fichier seul
);
}
interface
uses Windows, SysUtils, Classes, Masks;
procedure FindFiles(Directory: string; FilesMask: string; aList: TStrings; const
SubFolders: Boolean = True; const FullPath: Boolean = True);
implementation
uses Forms, Controls;
function MatchesMasks(const Filename, Mask: string): Boolean;
var
CMask: TMask;
MaskList: TStringList;
I: Integer;
begin
Result := False;
MaskList := TStringList.Create;
try
MaskList.Delimiter := ';';
MaskList.DelimitedText := Mask;
for I := 0 to MaskList.Count - 1 do
begin
if Result then Break;
CMask := TMask.Create(MaskList[I]);
try
Result := CMask.Matches(Filename);
finally
CMask.Free;
end;
end;
finally
MaskList.Free;
end;
end;
procedure _FindFiles(Directory: string; FilesMask: string; aList: TStrings; const
SubFolders: Boolean = True; const FullPath: Boolean = True);
var
SearchRec: TSearchRec;
begin
if not DirectoryExists(Directory) then
Exit;
if Directory[Length(Directory)] <> '\' then Directory := Directory + '\';
if FindFirst(Directory + '*.*', faAnyFile, SearchRec) = 0 then
try
aList.BeginUpdate;
repeat
if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1]
<> '.') then
begin
if (SearchRec.Attr and faDirectory > 0) and SubFolders then
_FindFiles(Directory + SearchRec.Name, FilesMask, aList, SubFolders,
FullPath);
end
else
if (SearchRec.Name[1] <> '.') and (MatchesMasks(SearchRec.Name,
FilesMask)) then
if FullPath then
aList.Add(Directory + SearchRec.Name)
else
aList.Add(SearchRec.Name);
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
aList.EndUpdate;
end;
end;
procedure FindFiles(Directory: string; FilesMask: string; aList: TStrings; const
SubFolders: Boolean = True; const FullPath: Boolean = True);
begin
try
SetCursor(LoadCursor(0, IDC_WAIT));
_FindFiles(Directory, FilesMask, aList, SubFolders, FullPath);
finally
SetCursor(LoadCursor(0, IDC_ARROW));
end;
end;
end. |
Partager