读取目录下的所以文件,并返回所有的文件的路径到Memo里。并可以读取目录下的子目录里的文件出来。谢谢!

解决方案 »

  1.   

    procedure FileSearch(PathName:string);
    var
      F : TSearchRec;
      Found : Boolean;
    begin
      ChDir(PathName);
      Found := (FindFirst('*.*', faAnyFile, F) = 0);
      while Found do
      begin
        if (F.Name = '.') or (F.Name = '..') then
        begin
          Found := (FindNext(F) = 0);
          Continue;
        end;    if (F.Attr and faDirectory)>0 then
        begin
          Application.ProcessMessages;
          FileSearch(F.Name);
        end;
        //插入你的代码,F.Name就是文件名,GetCurrentDir可以得到当前目录
        Found := (FindNext(F) = 0);
      end;
      FindClose(F);
      ChDir('..\');
    end;
      

  2.   

    //得到某一文件夹下所有文件列表的函数 /////////////////////////////////////
    // getFileListFromDir('c:\windows',false,fileList);
                                //c:\windows or c:      ture|false
    procedure getFileListFromDir(dir:string;includeSubDir:boolean;var fileList:TStringList);
    var
      sr:TSearchRec;
      path:String;
    begin
      path:=dir+'\';      //c:\windows\
      dir:=dir+'\*.*';    //c:\windows\*.*
      if findFirst(dir,faanyfile,sr)=0 then
      repeat
      begin
        if (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)<>faDirectory) then
          fileList.Add(path+sr.Name);
        if (includeSubDir) and (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)=faDirectory) then
          getFileListFromDir(path+sr.Name,includeSubDir,fileList);
      end;
      until findNext(sr)<>0;
      findClose(sr);
    end;
      

  3.   

    再来一个:
    function GetFileList(path:string;Attr:integer;FileList:TStringList):boolean;
    var
       SearchRec: TSearchRec;
       i:integer;
    begin
         i:=FindFirst(path, attr, SearchRec);
         if i <> 0 then
         begin
              result := false;
              exit;
         end;     while i = 0 do
         begin
              FileList.Add(SearchRec.Name);
              i:=FindNext(SearchRec);
         end;
         FindClose(SearchRec);
         result := true;
    end;使用范例:
    var
       FileList:Tstringlist;
    begin
       filelist := Tstringlist.Create;
       getfilelist(ExtractFilePath(Application.ExeName)+'*.*',faAnyFile,filelist
       filelist.free;end;
      

  4.   

    以上函数获取 dir 中的文件,如果 include 为 true ,则包含所有的子目录中的文件,获取的文件列表存入 fileList 变量中。
      

  5.   

    来晚了但是也帖一个吧
    参数Disk是盘符,path是目录,fileresult是返回的字符列表()
    楼主直接引用findall(‘c:’,'\windows',Memo1.lines)就可以了如果有子目录在函数最后加个判断递归查询就可以了
    procedure findall(disk,path: String; var fileresult: Tstrings);
    var
    fpath: String;
    fs: TsearchRec;
    begin
    fpath:=disk+path+'\*.*';
    if findfirst(fpath,faAnyFile,fs)=0 then
    begin
    if (fs.Name<>'.')and(fs.Name<>'..') then
    if (fs.Attr and faDirectory)=faDirectory then
    findall(disk,path+'\'+fs.Name,fileresult)
    else
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas(
    strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
    while findnext(fs)=0 do
    begin
    if (fs.Name<>'.')and(fs.Name<>'..') then
    if (fs.Attr and faDirectory)=faDirectory then
    findall(disk,path+'\'+fs.Name,fileresult)
    else
    fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str
    pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
    end;
    end;
    findclose(fs);
    end;