Delphi

本类阅读TOP10

·分布式网络考试系统原型分析及实现
·游戏外挂设计技术探讨①
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·试题库开发中非文本数据的处理
·如何将几个DBGRID里的内容导入同一个EXCEL表中....的问题
·如何使用Delphi设计强大的服务器程序
·工人线程中关闭窗体的实现
·用DLL方式封装MDI子窗体。
·支持XP下托盘栏气球提示的托盘单元

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
两个delphi下遍历指定目录下指定类型文件的函数

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

by meiqingsong(阿飛)

// ================================================================
// 遍历某个文件夹下某种文件,
// 使用说明
//       _GetFileList(ListBox1.Items,'c:\*.doc');
//           _GetFileList(MyTStringList,'c:\*.exe');
// ================================================================
procedure TForm1._GetFileList(AStrings: TStrings ; ASourFile: string);
var sour_path,sour_file: string;
    TmpList:TStringList;
    FileRec:TSearchrec;
begin

   sour_path:=ExtractFilePath(ASourFile);
   sour_file:=ExtractFileName(ASourFile);

   if not DirectoryExists(sour_path) then
   begin
     AStrings.Clear;
     exit;
   end;

   TmpList:=TStringList.Create;
   TmpList.Clear;

   if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then
   repeat
      if ((FileRec.Attr and faDirectory) = 0) then
         begin
           TmpList.Add(sour_path+FileRec.Name)
         end;
   until FindNext(FileRec)<>0;

   SysUtils.FindClose(FileRec);

   AStrings.Assign(TmpList);

   TmpList.Free;
end;

// ================================================================
// 遍历某个文件夹及子文件夹下某种文件,
// 使用说明
//       _GetFileList(ListBox1.Items, 'c:\', '*.doc');
//           _GetFileList(MyTStringList, 'c:\', '*.exe');
// ================================================================
procedure _GetFileList(AStrings: TStrings; ASourFile,
  FileName: string);
var sour_path,sour_file: string;
    TmpList:TStringList;
    FileRec, subFileRec:TSearchrec;
    i: Integer;
begin
   if rightStr(trim(ASourFile), 1) <> '\' then
     sour_path :=trim(ASourFile) + '\'
   else
     sour_path :=trim(ASourFile);
   sour_file:= FileName;

   if not DirectoryExists(sour_path) then
   begin
     AStrings.Clear;
     exit;
   end;

   TmpList:=TStringList.Create;
   TmpList.Clear;

   if FindFirst(sour_path+'*.*',faAnyfile,FileRec) = 0 then
   repeat
      if ((FileRec.Attr and faDirectory) <> 0) then
         begin
           if ((FileRec.Name<> '.') and (FileRec.Name  <> '..')) then
             _GetFileList(AStrings, sour_path+ FileRec.Name + '\',  sour_file);
         end
      else
        if FindFirst(sour_path + FileName,faAnyfile,subFileRec) = 0 then
        repeat
          if ((subFileRec.Attr and faDirectory) = 0) then
            TmpList.Add(sour_path+subFileRec.Name);
        until FindNext(subFileRec)<>0;

   until FindNext(FileRec)<>0;

   SysUtils.FindClose(FileRec);
   for i := 0 to TmpList.Count -1 do
     AStrings.Add(TmpList.Strings[i]);

   TmpList.Free;
end;

 




相关文章

相关软件