我想对一个目录下的文件都进行同样的操作,但是这些文件每次都不同。如何做到遍历一个目录下的所有文件?
不包含子目录应该怎么做,包含子目录又应该怎么做?

解决方案 »

  1.   

    不包含子目录
    var
       f:TSearchRec;
    begin
       if FindFirst('*.*',$0000003F,f)=0 then
       begin
          //do something here   
          while FindNext(f) = 0 do
          begin
          //do somethine here
          end;
         FindClose(f);
       end;
    end;
    包含子目录的自己再改改就能用了
      

  2.   


    uses ShellAPI;
    function DoOpenFile(sDirName:String;Flag:Boolean):Boolean;
    var
       hFindFile:Cardinal;
       F:Integer;
       //hFindFile:TSearchRec;
       tfile,FileName:String;
       sCurDir:String[255];
       FindFileData:WIN32_FIND_DATA;
       CreateFT, LastAccessFT, LastWriteFT: TFileTime;
       ST: TSystemTime;
       ST1:TDateTime;
    begin
      sCurDir:=GetCurrentDir;
      ChDir(sDirName);
      hFindFile:=FindFirstFile('*.*',FindFileData);
      if hFindFile<>INVALID_HANDLE_VALUE then
      begin
        repeat
          tfile:=FindFileData.cFileName;
          if (tfile='.') or (tfile='..') then
             Continue;
          if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
          begin
            if sDirName[Length(sDirName)]<>'\' then
              FileName:=sDirName+'\'+tfile
            else
              FileName:=sDirName+tfile;
              DoOpenFile(FileName,Flag);//第归      end else
          begin
            if Flag=False then
            begin
              F := CreateFile(Pchar(sDirName+'\'+tfile), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
              if F=INVALID_HANDLE_VALUE then
              begin
                ShowMessage('Can not open file!');
                Result:=False;
                Exit;
              end;
    //以下处理遍厉到的每个文件
                if GetFileTime(F,@CreateFT, @LastAccessFT, @LastWriteFT) then
                begin
                  FileTimeToSystemTime(LastWriteFT, ST);              ST1:=StrToDate(IntToStr(ST.wYear)+'-'+IntToStr(ST.wMonth)+'-'+IntToStr(ST.wDay));
                  CloseHandle(F);              if ST1-now >FilterDay then
                  begin
                    if MoveData(tfile,sDirName)=False then Continue;  
                  end else
                    Continue
                end else
                begin
                  Continue;
                  CloseHandle(F);
                end;
              end else
                if MoveData(tfile,sDirName)=False then Continue;  
            end;
    //处理完当前文件  
        until FindNextFile(hFindFile,FindFileData)=False;
        Windows.FindClose(hFindFile);
      end else
      begin
        ChDir(sCurDir);
        Result:=False;
        Exit;
      end;
      ChDir(sCurDir);
      Result:=True;
    end;
      

  3.   

    //use shellapi;
    procedure findall(path:string;findsubdir:boolean;var fileresult:TStrings);
    var
    iCnt:integer;
    sr:TSearchRec;
    begin
    iCnt:=FindFirst(path+'\*.*',faAnyFile,sr);
    while iCnt=0 do
    begin
    if sr.name[1]<>'.' then//掠过'.' '..'
    if (sr.attr and faDirectory)=faDirectory then//目录
    begin
    if findsubdir then
    findall(path+'\'+sr.name,findsubdir,fileresult);//递归找子目录
    end
    else
    fileresult.add(path+'\'+sr.name);//增加文件
    iCnt:=FindNext(sr);
    end;
    FindClose(sr);
    end;
    procedure button1click(Sender:TObject);
    var
    tmp:TStings;
    begin
    tmp:=TStringList.create;
    findall('C:\test',false,tmp);//找C:\test下的除目录文件以外的文件,且不的子目录
    memo1.lines.assign(tmp);
    tmp.free;
    end;
      

  4.   

    用APISHFileOperation(LPSHFILEOPSTRUCT lpFileOp );
    也可以