delphi 遍历所有目录和子目录下的文件名(可按后缀名遍历)


本文整理自网络,侵删。

 

procedure EnumFileInQueue(path: PChar; fileExt: string; fileList: TStringList);
var
   searchRec: TSearchRec;
   found: Integer;
   tmpStr: string;
   curDir: string;
   dirs: TQueue;
   pszDir: PChar;
begin
   dirs := TQueue.Create; //创建目录队列
   dirs.Push(path); //将起始搜索路径入队
   pszDir := dirs.Pop;
   curDir := StrPas(pszDir); //出队
   {开始遍历,直至队列为空(即没有目录需要遍历)}
   while (True) do
   begin
      //加上搜索后缀,得到类似'c:/*.*' 、'c:/windows/*.*'的搜索路径
      tmpStr := curDir + '/*.*';
      //在当前目录查找第一个文件、子目录
      found := FindFirst(tmpStr, faAnyFile, searchRec);
      while found = 0 do //找到了一个文件或目录后
      begin
          //如果找到的是个目录
         if (searchRec.Attr and faDirectory) <> 0 then
         begin
          {在搜索非根目录(C:/、D:/)下的子目录时会出现'.','..'的"虚拟目录"
          大概是表示上层目录和下层目录吧。。。要过滤掉才可以}
            if (searchRec.Name <> '.') and (searchRec.Name <> '..') then
            begin
               {由于查找到的子目录只有个目录名,所以要添上上层目录的路径
                searchRec.Name = 'Windows';
                tmpStr:='c:/Windows';
                加个断点就一清二楚了
               }
               tmpStr := curDir + '/' + searchRec.Name;
               {将搜索到的目录入队。让它先晾着。
                因为TQueue里面的数据只能是指针,所以要把string转换为PChar
                同时使用StrNew函数重新申请一个空间存入数据,否则会使已经进
                入队列的指针指向不存在或不正确的数据(tmpStr是局部变量)。}
               dirs.Push(StrNew(PChar(tmpStr)));
            end;
         end
         else //如果找到的是个文件
         begin
             {Result记录着搜索到的文件数。可是我是用CreateThread创建线程
              来调用函数的,不知道怎么得到这个返回值。。。我不想用全局变量}
            //把找到的文件加到Memo控件
            if fileExt = '.*' then
               fileList.Add(curDir + '/' + searchRec.Name)
            else
            begin
               if SameText(RightStr(curDir + '/' + searchRec.Name, Length(fileExt)), fileExt) then
                  fileList.Add(curDir + '/' + searchRec.Name);
            end;
         end;
          //查找下一个文件或目录
         found := FindNext(searchRec);
      end;
      {当前目录找到后,如果队列中没有数据,则表示全部找到了;
        否则就是还有子目录未查找,取一个出来继续查找。}
      if dirs.Count > 0 then
      begin
         pszDir := dirs.Pop;
         curDir := StrPas(pszDir);
         StrDispose(pszDir);
      end
      else
         break;
   end;
   //释放资源
   dirs.Free;
   FindClose(searchRec);
end;

//调用
procedure TForm1.Button1Click(Sender: TObject);
var
   dir: string;
   FileNameList: TStringList;
begin
   dir := trim(Edit1.Text);
   FileNameList := TStringList.Create;
   //EnumFileInQueue(PChar(dir), '.*', FileNameList);
   EnumFileInQueue(PChar(dir), '.*', FileNameList);
   ShowMessage(IntToStr(FileNameList.Count));
   FileNameList.Free;
end;

 

相关阅读 >>

xe7提示找不到sharedactivitycontext函数

Delphi之猥琐的webserver实现

Delphi窗口显示和关闭的时候出现动画效果

Delphi string.split 按照任意字符串分割语句

Delphi中url的汉字编码

Delphi读写文本文件 assignfile reset append

Delphi 如何从url提取文件名?

Delphi动态创建一个鼠标指针图案

Delphi xe webbroker 开发,解决 response 返回中文乱码问题

Delphi psafearray与tbytes类型转换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...