码迷,mamicode.com
首页 > Windows程序 > 详细

delphi获取目录下所有文件名

时间:2016-08-07 15:21:43      阅读:467      评论:0      收藏:0      [点我收藏+]

标签:

procedure  searchfile(path:string);//注意,path后面要有‘\‘;   
   var   
       SearchRec:TSearchRec;   
       found:integer;   
   begin   
       found:=FindFirst(path+‘*.*‘,faAnyFile,SearchRec);   
       while    found=0    do   
         begin   
             if (SearchRec.Name<>‘.‘)  and (SearchRec.Name<>‘..‘)     
                   and    (SearchRec.Attr<>faDirectory)    then   
                 ListBox1.Add(SearchRec.Name);   
             found:=FindNext(SearchRec);   
         end;   
       FindClose(SearchRec);   
   end;  

//===============================================================

上面一个只获取当前目录下所有文件,如果要获取该目录下的子目录的文件,看下面   
   procedure  SearchFile(path:string);   
   var   
       SearchRec:TSearchRec;   
       found:integer;   
   begin   
       found:=FindFirst(path+‘*.*‘,faAanyFile,SearchRec);   
       while    found=0    do   
         begin   
           if  (SearchRec.Name<>‘.‘)  and  (SearchRec.name<>‘..)  and   
                 (SearchRec.Name=faDirectory)    then   
               SearchFile(SearchRec.Name+‘\‘)   
           else   
               LisBox1.Add(SearchRec.Name);   
           found:=FindNext(SearchREc);   
       end;   
       FindClose(SearchRec);   
   end;   
//===================================================================

 

第二个函数的 SearchRec.Name=faDirectory 错了

procedure TForm2.SearchFile(path:string; strList: TStrings);
var
SearchRec:TSearchRec;
found:integer;
begin
found:=FindFirst(path+‘*.*‘, faAnyFile,SearchRec);
while found=0 do
begin
if (SearchRec.Name<>‘.‘) and (SearchRec.name<>‘..‘) and
((SearchRec.Attr and faDirectory) <> 0) then
SearchFile(path + SearchRec.Name + ‘\‘, strList)
else
strList.Add(path + SearchRec.Name);
found:=FindNext(SearchREc);
end;
FindClose(SearchRec);
end;

 

delphi获取目录下所有文件名

标签:

原文地址:http://www.cnblogs.com/zhangzhifeng/p/5746026.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!