1 { Version 050519. Copyright � Alexey A.Chernobaev, 2003-5 }
2 
3 unit VFileLst;
4 
5 interface
6 
7 {$I VCheck.inc}
8 
9 uses
10   {$IFDEF V_WIN}Windows,{$ENDIF}
11   SysUtils, ExtType, ExtSys, StrLst, VectStr, VFileSys;
12 
13 procedure ForceDeleteFiles(List: TStrLst);
14 procedure ForceDeleteFilesOrDirs(List: TStrLst);
15 { ��������� ForceDeleteFile... �� ���� ��������� List }
16 { applies ForceDelete... to all elements of List }
17 
18 procedure ForceDeleteDirs(List: TStrLst; SubDirs: Boolean);
19 { ������� ���������� �� ������ List; ��� SubDirs = True ���������� �������
20   ����� ��� �� ������������� }
21 { deletes all directories from List; if SubDirs = True then recursively deletes
22   all their subdirectories }
23 
GetFileListnull24 function GetFileList(List: TStrLst; Path, Mask: String;
25   SubDirs: Boolean{$IFDEF V_D4} = False{$ENDIF}
26   {$IFDEF V_WIN}; FileExcludeAttrs: UInt16 = 0{$ENDIF}): Integer;
27 { ��������� ���������� Path (� ����� ��� ������������� Path ��� SubDirs = True)
28   � ���������� � List ������ ����� ���� ������, ��������������� ����� Mask
29   (����� �� ��������� - '*'); ���� FileExcludeAttrs <> 0, �� �����, � �������
30   ���������� ���� �� ���� �� ��������� FileExcludeAttrs, �� ���������� � ������
31   �����������; ���������� ���������� ��������� ������ }
32 { scans the directory Path (and all it's subdirectories if SubDirs = True) and
33   writes full names of files matching Mask (the default mask is '*') to List;
34   if FileExcludeAttrs <> 0 then files which have at least one attribute from
35   FileExcludeAttrs set will not be included into the result list; returns the
36   number of the files found }
37 
GetFileListExnull38 function GetFileListEx(List: TStrLst; Path, Mask: String;
39   SubDirs: Boolean{$IFDEF V_D4} = False{$ENDIF}): Boolean;
40 { ���� Path �������� ������ ����������, �� �������� ��� ��, ��� GetFileList,
41   ����� �������� GetFileList(List, ExtractFilePath(Path), ExtractFileName(Path),
42   SubDirs); ���������� True, ���� ���������� ���� ���������� Path, ����
43   ExtractFilePath(Path), � False - ����� }
44 { if Path is a directory name then works in the same way as GetFileList else
45   calls GetFileList(List, ExtractFilePath(Path), ExtractFileName(Path), SubDirs);
46   returns True if either Path or ExtractFilePath(Path) directory exist and
47   False otherwise }
48 
49 {$IFDEF UNIX}
GetAnyCaseFileNamenull50 function GetAnyCaseFileName(const FileName: String): String;
51 {$ENDIF}
52 
53 implementation
54 
55 {$IFDEF V_D6}{$IFDEF V_WIN}{$IFDEF NOWARN}
56   {$WARN SYMBOL_PLATFORM OFF}
57 {$ENDIF}{$ENDIF}{$ENDIF}
58 
59 procedure ForceDeleteFiles(List: TStrLst);
60 var
61   I: Integer;
62 begin
63   for I:=0 to List.Count - 1 do
64     DeleteFile(List[I]);
65 end;
66 
67 procedure ForceDeleteFilesOrDirs(List: TStrLst);
68 var
69   I: Integer;
70 begin
71   for I:=0 to List.Count - 1 do
72     DeleteFile{OrDir}(List[I]);
73 end;
74 
75 procedure ForceDeleteDirs(List: TStrLst; SubDirs: Boolean);
76 
77   procedure DeleteDir(Path: String);
78   var
79     I: Integer;
80     Dirs: TStrLst;
81   begin
82     if SubDirs then begin
83       Path:=IncludeTrailingPathDelimiter(Path);
84       Dirs:=TStrLst.Create;
85       try
86         //GetDirList(Dirs, Path, '');
87         for I:=0 to Dirs.Count - 1 do
88           DeleteDir(Path + Dirs[I]);
89       finally
90         Dirs.Free;
91       end;
92     end;
93     DeleteDir(Path);
94   end;
95 
96 var
97   I: Integer;
98 begin
99   List.SortDesc;
100   for I:=0 to List.Count - 1 do
101     DeleteDir(List[I]);
102 end;
103 
GetExtLennull104 function GetExtLen(const Mask: String): Integer;
105 begin
106   Result:=LastPos('.', Mask);
107   if (Result > 0) and (CharPos('*', Mask, Result) = 0) then
108     Result:=Length(Mask) - Result
109   else
110     Result:=-1;
111 end;
112 
GetFileListnull113 function GetFileList(List: TStrLst; Path, Mask: String; SubDirs: Boolean
114   {$IFDEF V_WIN}; FileExcludeAttrs: UInt16{$ENDIF}): Integer;
115 {$IFDEF V_WIN}
116 var
117   MaskExtLen: Integer;
118 {$ENDIF}
119 
120   procedure AddToList(const CurPath: String);
121   var
122     I: Integer;
123     {$IFDEF V_WIN}
124     hFind: THandle;
125     S: String;
126     FindData: TWin32FindData;
127     {$ENDIF}
128     {$IFDEF UNIX}
129     SR: TSearchRec;
130     {$ENDIF}
131     SubDirList: TStrLst;
132   begin
133     {$IFDEF V_WIN}
134     hFind:=FindFirstFile(PChar(CurPath + Mask), FindData);
135     if hFind <> INVALID_HANDLE_VALUE then
136     try
137       repeat
138         // ������: ��� Mask = '*.txt' �������� '*.txt1' => ��������� �� MaskExtLen
139         // sometimes we get '*.txt1' when Mask = '*.txt' => check by MaskExtLen
140         if FindData.dwFileAttributes and FileExcludeAttrs = 0 then begin
141           S:=LString(@FindData.cFileName, SizeOf(FindData.cFileName));
142           if (MaskExtLen < 0) or (MaskExtLen = Length(S) - LastPos('.', S)) then
143             List.Add(CurPath + S);
144         end;
145       until not FindNextFile(hFind, FindData);
146     finally
147       Windows.FindClose(hFind);
148     end;
149     {$ENDIF}
150     {$IFDEF UNIX}
151     if FindFirst(CurPath + Mask,
152       faAnyFile and not (faDirectory{$IFDEF V_WIN} or faVolumeID{$ENDIF}), SR) = 0 then
153     try
154       repeat
155         {$IFDEF V_WIN}
156         if MaskExtLen >= 0 then begin
157           I:=LastPos('.', SR.Name);
158           if I > 0 then
159             I:=Length(SR.Name) - I;
160           if MaskExtLen <> I then
161             Continue;
162         end;
163         {$ENDIF}
164         List.Add(CurPath + SR.Name);
165       until FindNext(SR) <> 0;
166     finally
167       SysUtils.FindClose(SR);
168     end;
169     {$ENDIF}
170     if SubDirs then begin
171       SubDirList:=TStrLst.Create;
172       try
173         //GetDirList(SubDirList, CurPath, '*');
174         for I:=0 to SubDirList.Count - 1 do
175           AddToList(CurPath + SubDirList.Items[I] + PathDelim);
176       finally
177         SubDirList.Free;
178       end;
179     end;
180   end;
181 
182 begin
183   List.Clear;
184   if Path <> '' then begin
185     {$IFDEF V_WIN}
186     FileExcludeAttrs:=FileExcludeAttrs or FILE_ATTRIBUTE_DIRECTORY;
187     MaskExtLen:=GetExtLen(Mask);
188     {$ENDIF}
189     if Mask = '' then
190       Mask:='*';
191     AddToList(IncludeTrailingPathDelimiter(Path));
192   end;
193   Result:=List.Count;
194 end;
195 
GetFileListExnull196 function GetFileListEx(List: TStrLst; Path, Mask: String; SubDirs: Boolean): Boolean;
197 var
198   L: Integer;
199 begin
200   if not DirectoryExists(Path) then begin
201     Result:=False;
202     Mask:=ExtractFileName(Path);
203     L:=Length(Path) - Length(Mask) - 1;
204     if L <= 0 then
205       Exit;
206     SetLength(Path, L);
207     if not DirectoryExists(Path) then
208       Exit;
209   end;
210   GetFileList(List, Path, Mask, SubDirs);
211   Result:=True;
212 end;
213 
214 {$IFDEF V_WIN}
GetDirListnull215 function GetDirList(List: TStrLst; const Path: String; Mask: String
216   {$IFDEF V_WIN}; DirExcludeAttrs: UInt16{$ENDIF}): Integer;
217 var
218   MaskExtLen: Integer;
219   S: String;
220   hFind: THandle;
221   FindData: TWin32FindData;
222 begin
223   if Path = '' then begin
224     Result:=0;
225     Exit;
226   end;
227   List.Clear;
228   MaskExtLen:=GetExtLen(Mask);
229   if Mask = '' then
230     Mask:='*';
231   hFind:=FindFirstFile(PChar(IncludeTrailingPathDelimiter(Path) + Mask), FindData);
232   if hFind <> INVALID_HANDLE_VALUE then
233   try
234     repeat
235       if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0) and
236         (FindData.dwFileAttributes and DirExcludeAttrs = 0) then
237       begin
238         S:=LString(@FindData.cFileName, SizeOf(FindData.cFileName));
239         if (S <> '.') and (S <> '..') and
240           ((MaskExtLen < 0) or (MaskExtLen = Length(S) - LastPos('.', S)))
241         then
242           List.Add(S);
243       end;
244     until not FindNextFile(hFind, FindData);
245   finally
246     Windows.FindClose(hFind);
247   end;
248   Result:=List.Count;
249 end;
250 {$ENDIF}
251 
252 {$IFDEF UNIX}
GetDirListnull253 function GetDirList(List: TStrLst; const Path: String; Mask: String): Integer;
254 var
255   I, MaskExtLen: Integer;
256   SR: TSearchRec;
257 begin
258   List.Clear;
259   MaskExtLen:=GetExtLen(Mask);
260   if Mask = '' then
261     Mask:='*';
262   if FindFirst(IncludeTrailingPathDelimiter(Path) + Mask,
263     {$IFDEF V_WIN}faReadOnly + faHidden + faArchive + {$ENDIF}faDirectory, SR) = 0 then
264   try
265     repeat
266       if (SR.Attr and faDirectory <> 0) and
267         (SR.Name <> '.') and (SR.Name <> '..') then
268       begin
269         if MaskExtLen >= 0 then begin
270           I:=LastPos('.', SR.Name);
271           if I > 0 then
272             I:=Length(SR.Name) - I;
273           if MaskExtLen <> I then
274             Continue;
275         end;
276         List.Add(SR.Name);
277       end;
278     until FindNext(SR) <> 0;
279   finally
280     SysUtils.FindClose(SR);
281   end;
282   Result:=List.Count;
283 end;
284 
GetAnyCaseFileNamenull285 function GetAnyCaseFileName(const FileName: String): String;
286 var
287   I: Integer;
288   F1: Char;
289   FilePath: String;
290   Lst: TStrLst;
291 begin
292   if FileExists(FileName) then begin
293     Result:=FileName;
294     Exit;
295   end;
296   Result:='';
297   FilePath:=ExtractFilePath(FileName);
298   I:=Length(FilePath);
299   if I >= Length(FileName) then
300     Exit;
301   F1:=LoCase(FileName[I + 1]);
302   Lst:=TStrLst.Create;
303   try
304     //GetFileList(Lst, FilePath, F1 + '*');
305     I:=Lst.IndexOf(FileName);
306     if I >= 0 then begin
307       Result:=Lst[I];
308       Exit;
309     end;
310     //GetFileList(Lst, FilePath, UpCase(F1) + '*');
311     I:=Lst.IndexOf(FileName);
312     if I >= 0 then
313       Result:=Lst[I];
314   finally
315     Lst.Free;
316   end;
317 end;
318 {$ENDIF}
319 
320 end.
321