我现在想读取本地目录下的文件(包括文件名和内容)及子目录下的文件,应该怎么做?最好有源程序。

解决方案 »

  1.   

    procedure FindFiles(Apath: String);
    var
       FSearchRec:TSearchRec;
       DSearchRec:TSearchRec;
       FindResult:integer;
       function isDirNotation(ADirName:String):Boolean;
       begin
          Result:=(ADirName='.') or (ADirName='..');
       end;  
    begin
       aPath:=GetDirectoryName(APath);
       FIndResult:=FindFirst(APath+FFileName,faAnyFile+faHidden+faSysFile+faReadOnly,FSearchRec);
       try
           while FindResult =0 do
           begin
              lbfiles.Items.Add(LowerCase(Apath+FSearchRec.Name));
              FindResult:=FindNext(FSearchRec);
           end;       FindResult:=FindFirst(APath+'*.*',faDirectory,DsearchRec);       while FindResult=0 do
           begin
               if ((DSearchRec.Attr and faDirectory)=faDirectory) and not isDirNotation(DsearchRec.Name) then
                 FindFiles(Apath+DSearchRec.Name);
               FindResult:=FindNext(DSearchRec);       end;       finally
              FindClose(FSearchRec);
           end;
    end;
      

  2.   

    是遍历目录吗?用递归吧
    参考这个deltree的functionfunction Deltree(Folder: string): Boolean;
    var
      SearchRec: TSearchRec;
      Attr: Integer;
    begin {Deltree function}
      Result := False;
      if Copy(Folder, Length(Folder), 1) = '.' then
      begin
        Result := True;
        Exit;
      end;
      if DirectoryExists(Folder) then
      begin
        if Copy(Folder, Length(Folder), 1) <> '\' then Folder := Folder + '\';
        if FindFirst(Folder + '*.*', faAnyFile, SearchRec) = 0 then
        begin
          if not Deltree(Folder + SearchRec.Name) then Exit;
          while FindNext(SearchRec) = 0 do
            if not Deltree(Folder + SearchRec.Name) then Exit;
        end;
        FindClose(SearchRec);
            if forcedel then
            rkey:='y'
            else
            begin
            write('Are you sure remove DIR[sub dir]:'+Folder+ ' (n/y):');
            rkey:='';
            readln(rkey);
            end;        if rkey='y' then
            begin
            if not RemoveDir(Folder) then
            begin
            ndfcount:=ndfcount+1;
            writeln(Folder+ '<dir> remove fail!');
            Exit
            end
            else
            dfcount:=dfcount+1;
            end;
      end
      else if FileExists(Folder) then
      begin
        if forcedel then
            rkey:='y'
            else
            begin
            write('Are you sure delete file[sub dir]:'+Folder+ ' (n/y):');
            rkey:='';
            readln(rkey);
            end;    if rkey='y' then
        if not DeleteFile(Folder) then //Maybe readonly or in use
        begin
          Attr := FileGetAttr(Folder);
          if Odd(Attr) then //If have readonly attribute 1
          begin
            Dec(Attr); //Get ride of readonly
            FilesetAttr(Folder, Attr); //Set it
            if not DeleteFile(Folder) then 
            begin
            ndfcount:=ndfcount+1;
            writeln(Folder+ ' deleted fail!');
            Exit; //Delete it again
            end
            else
            dfcount:=dfcount+1;
          end;
        end;  end
      else
      begin
      ndfcount:=ndfcount+1;
      writeln(Folder+ '<dir> not exist!');
      end;
      Result:= True;
    end;
      

  3.   

    wycg_cnh20(从Delphi飞向C++的笨鸟) 
    FFileName 是什么?
      

  4.   

    不就是递归的
    Google上N多例子http://lysoft.7u7.net
      

  5.   

    这种问题,我在CSDN上回答过好多遍啦~~~用CSDN的全文搜索
      

  6.   

    回复人: ikumei(雷欧钠德) ( ) 信誉:106  2004-10-10 12:21:27  得分: 0  
     
     
       
    最近一直天天加班 回去也没开delphi的机会了
    贴出我的代码如下,写的不好请见谅了
    但经过测试全部能够使用,如发现问题,请通知我 哈
    unit sFiles;interfaceuses Windows, SysUtils, Classes, Registry, ShellAPI, SHFolder;type
      TEnumDirectoryFileProc = procedure (Filename: string; var bContinue: Boolean) of object;procedure EnumDirectoryFiles(const sDir : String;
                                 const FileName: String;
                                 Attr: Integer;
                                 IncludeSubs : boolean;
                                 Callback : TEnumDirectoryFileProc);procedure EnumAllFiles(const sDir : String;
                           Attr: Integer;
                           IncludeSubs : boolean;
                           Callback : TEnumDirectoryFileProc);
    procedure EnumDirectoryFiles(const sDir : String;
                                 const Filename: String;
                                 Attr: Integer;
                                 IncludeSubs : boolean;
                                 Callback : TEnumDirectoryFileProc);
    var
      list: TStrings;
      SearchRec :TSearchRec;
      Result : LongInt;
      bc: Boolean;
    begin
      list := TStringList.Create;
      list.Add(sDir);
      if not IncludeSubs then
      begin
        Result := FindFirst(list.Strings[0]+'\'+Filename, Attr, SearchRec);
        while Result = 0 do
        begin
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) = 0 then
            begin
              { Call are callback function}
              Callback(list.Strings[0]+'\'+SearchRec.name, bc);
              if not bc then
              begin
                SysUtils.FindClose(SearchRec);
                exit;
              end;
            end;
          end; //end if
          if not bc then
          begin
            SysUtils.FindClose(SearchRec);
            exit;
          end;
          Result := FindNext(SearchRec);
        end;
        SysUtils.FindClose(SearchRec);
        exit;
      end;  while list.Count<>0 do
      begin
        Result := FindFirst(list.Strings[0]+'\'+Filename, Attr, SearchRec);
        while Result = 0 do
        begin
          { This makes sure its not the . or .. directorys}
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) = 0 then
            begin
              { Call are callback function}
              Callback(list.Strings[0]+'\'+SearchRec.name, bc);
              if not bc then
              begin
                SysUtils.FindClose(SearchRec);
                exit;
              end;
            end;
          end; //end if
          Result := FindNext(SearchRec);
        end;  //end while
        SysUtils.FindClose(SearchRec);
        Result := FindFirst(list.Strings[0]+'\*.*', faAnyFile, SearchRec);
        while Result = 0 do
        begin
          { This makes sure its not the . or .. directorys}
          if not (SearchRec.name[1]='.') then
          begin
            if (SearchRec.Attr and faDirectory) <> 0 then
            begin
              list.Add(list.Strings[0]+'\'+SearchRec.Name);
            end;
          end; //end if
          Result := FindNext(SearchRec);
        end;  //end while
        SysUtils.FindClose(SearchRec);
        list.Delete(0);
      end;  list.Free;
    end;procedure EnumAllFiles(const sDir : String;
                           Attr: Integer;
                           IncludeSubs : boolean;
                           Callback : TEnumDirectoryFileProc);
    var
      SearchRec :TSearchRec;
      Result : LongInt;
      bc: Boolean;
    begin
      try
      Result := FindFirst(sDir+'\'+'*.*', Attr, SearchRec);
      while Result = 0 do
      begin
        { This makes sure its not the . or .. directorys}
        if SearchRec.name[1]<>'.' then
        begin
          if (SearchRec.Attr and faDirectory) <> 0 then
          begin
            {its a dir so do a recursive call if subdirectorys wanted}
            if IncludeSubs then
              EnumAllFiles(sDir+'\'+SearchRec.name, Attr, IncludeSubs, callback);
          end
          else
          begin
            { Call are callback function}
            Callback(sDir+'\'+SearchRec.name, bc);
            if not bc then break;
          end
        end; //if . ..
        Result := FindNext(SearchRec);
      end;  //end while
      finally
      SysUtils.FindClose(SearchRec);
      end;
    end;end.由于是日文系统 注释全变成乱麻了。。  
     
    Top  
     
     回复人: ikumei(雷欧钠德) ( ) 信誉:106  2004-10-10 12:38:52  得分: 0  
     
     
       
    使用时这样:
    //当每次枚举到一个文件或文件夹的时候会自动调用这个函数
    //Filename是枚举到的文件或文件夹的名称,包含完整路径
    //bContinue通知枚举函数是否继续枚举文件或文件夹。为false的时候将停止继续枚举
    procedure mycallback(Filename: string; var bContinue: Boolean);
    begin
      //在这里对枚举到的文件进行操作
    end;EnumDirectoryFiles("c:\","test.bat",faReadonly,true, mycallback)
    //这个将搜索c:\下面名字为test.bat的并且文件属性是只读的所有文件,包括c:\下的所有子目录EnumAllFiles("c:\", faReadonly,true,mycallback)
    //这个将枚举c:\下所有文件属性为只读的文件,搜索包括c:\下的所有子目录EnumDirectoryFiles("c:\","*.*",faReadonly,true, mycallback)

    EnumAllFiles("c:\", faReadonly,true,mycallback)
    效果是一样的文件属性的相关内容可以参考borland的帮助文件
    提示:在原代码中找到fareadonly(这可以通过右键菜单做到),就可在其附近找到所有文件属性相关常量
      

  7.   

    刚好用上了,谢谢ikumei(雷欧钠德) (