文件目录搜索函数 delphi

 

最近用C#做一个根据自定义条件Search file 的工具, 查找相关方面的资料:

function GetFilenames(FilePath,ExtMask: String):TStrings;   //遍历目录
var
  FileRec :TSearchrec;
begin
  if DirectoryExists(FilePath) then
  begin
    if FilePath[Length(FilePath)] <> '\' then FilePath := FilePath + '\';
    Result := TStringList.Create;
    if FindFirst(FilePath + ExtMask,faAnyfile,FileRec) = 0 then
    repeat
      if (FileRec.Attr and faDirectory) = 0 then
          Result.Add(FilePath + FileRec.Name);
    until FindNext(FileRec) <> 0;
    FindClose(FileRec);
  end;
end;


function GetFilenamesEx(FilePath,ExtMask :String):TStrings;   //遍历目录及子目录
var
  FileRec :TSearchrec;
begin
  if DirectoryExists(FilePath) then
  begin
    if FilePath[Length(FilePath)] <> '\' then FilePath := FilePath + '\';
    Result := TStringList.Create;
    if FindFirst(FilePath + ExtMask,faAnyfile,FileRec) = 0 then
    repeat
      if (FileRec.Attr and faDirectory) <> 0 then
      begin
        if (FileRec.Name<> '.') and (FileRec.Name  <> '..') then
          Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\',ExtMask));
        end
      else Result.Add(FilePath + FileRec.Name);
    until FindNext(FileRec) <> 0;
    FindClose(FileRec);
  end;
end;

posted @ 2009-05-27 23:11  novel  阅读(481)  评论(0)    收藏  举报