8月 9th, 2008

遍历指定目录下的所有文件

Category: 程序开发, Author: admin, Popularity: 5%

Delphi常用代码

////////////////////////////////////////////////////////////////////////////////
//遍历指定目录下的所有文件
////////////////////////////////////////////////////////////////////////////////
procedure FindFiles(APath, AFile: string;Strings1:Tstrings);
var
FindResult: integer;
FSearchRec, DSearchRec: TSearchRec;
function IsDirNotation(ADirName: string): Boolean;
begin
Result := ((ADirName = ‘.’) or (ADirName = ‘..’));
end;
begin
if APath[Length(APath)] <> ‘\’ then
APath := APath + ‘\’;
FindResult := FindFirst(APath + AFile, faAnyFile + faHidden +faSysFile + faReadOnly, FSearchRec); //在根目录中查找指定文件
try
while FindResult = 0 do
begin
Strings1.Add(APath + FSearchRec.Name);
FindResult := FindNext(FSearchRec); // 查找下一个指定文件
end;
FindResult := FindFirst(APath + ‘*.*’, faDirectory, DSearchRec); //进入当前目录的子目录继续查找
while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not IsDirNotation(DSearchRec.Name) then
FindFiles(APath + DSearchRec.Name, AFile,Strings1);   //递归调用FindFiles函数
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;

相关日志

Tags:.
评论数量() | Add Comments
本文网址:http://www.9usb.net/200808/delphi.html

There are No comments.

» You can leave a response

leave a reply