function   DeletePath(mDirName:   string):   Boolean;   
  var
      vSearchRec:   TSearchRec;
      vPathName:   string;
      K:   Integer;
  begin
      Result   :=   True;
      vPathName   :=   mDirName   +   '\*.*';
      K   :=   FindFirst(vPathName,   faAnyFile,   vSearchRec);
      while   K   =   0   do   begin
          if   (vSearchRec.Attr   and   faDirectory   >   0)   and
              (Pos(vSearchRec.Name,   '..')   =   0)   then   begin
              FileSetAttr(mDirName   +   '\'   +   vSearchRec.Name,   faDirectory);
              Result   :=   DeletePath(mDirName   +   '\'   +   vSearchRec.Name);
          end   else   if   Pos(vSearchRec.Name,   '..')   =   0   then   begin
              FileSetAttr(mDirName   +   '\'   +   vSearchRec.Name,   0);
              Result   :=   DeleteFile(PChar(mDirName   +   '\'   +   vSearchRec.Name));
          end;
          if   not   Result   then   Break;
          K   :=   FindNext(vSearchRec);
      end;
      FindClose(vSearchRec);
      Result   :=   RemoveDir(mDirName);
  end;   {   DeletePath   }
procedure TForm1.Button1Click(Sender: TObject);
begin
DeletePath('D:\日志\');
end;------------------------------------------------------------------------------这样可以删除全部文件.但改为*.log就不能对子目录删除了.我只想删除日志.怎么做?第二个就是.目录结构复制....------------------------------------------------------------------------------ function   CopyDirectory(SourcePath,TargetPath:   string):   Boolean;   
  var
      search:   TSearchRec;
      ret:   integer;
      key:   string;
  begin
      if   TargetPath[Length(TargetPath)]   <>   '\'   then
          TargetPath   :=   TargetPath   +   '\';
      if   SourcePath[Length(SourcePath)]   <>   '\'   then   
          SourcePath   :=   SourcePath   +   '\';      key   :=   SourcePath   +   '*.*';
      ret   :=   findFirst(key,   faanyfile,   search);
      while   ret   =   0   do   begin
          if   ((search.Attr   and   fadirectory)   =   faDirectory)
              then   begin
              if   (Search.Name   <>   '.')   and   (Search.name   <>   '..')   then
                  CopyDirectory(SourcePath   +   Search.name,TargetPath+search.Name);
          end   else   begin
              if   ((search.attr   and   fadirectory)   <>   fadirectory)   then
              begin
                  if   not   DirectoryExists(TargetPath)   then
                  ForceDirectories(targetPath);
                  copyfile(pchar(SourcePath+search.name),pchar(targetPath+search.Name),False);   
                  end;   
          end;
          ret   :=   FindNext(search);   
      end;   
      findClose(search);   
      result   :=   True;   
  end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyDirectory(pchar('\\192.168.0.254\net\945\'),pchar('d:\kk\'));
end;
这个也是如果我改了扩展名后又不能对子目录的文件复制了.例如我只想复制 *.dat 文件.

解决方案 »

  1.   

    有无人在啊!!
    delphi 发现越来越多问题存在了.很本来无错的都会不生效..
    例如 a:=a+b+c
    而C没有+上常有这样的事发生...汗.
      

  2.   

    两个问题都是一回事。两个解决办法:
    第一种:以*或*.*遍历所有目录项(Attr=faAnyFile),然后检查如果是目录则递归,否则如果其扩展名与你的目标扩展名( 可以用 MatchMask(Search.Name, '*.log') )相符,则执行相应的操作(如删除或复制)。
    第二种:两次遍历,第一次以你想要的文件名模式遍历目录项(当然也是FindFirst/FindNext),但要排除目录属性,即Attr参数不能包含faDirectory,进行你的操作后,再专门遍历属性为目录的项(Attr=faDirectory)。第一种只需一次遍历,第二种两次遍历,看起来比较罗索,但效率应该高一些(因为没有无关人员参与),你可以自己选择。--
    http://www.agui.googlepages.com
    mailto: agui.cn(a)gmail.com