//path='c:\data'
function TForm1.ClearDir( Path: string; Delete: Boolean): Boolean;
const
{$IFDEF WIN32}
  FileNotFound = 18;
{$ELSE}
  FileNotFound = -18;
{$ENDIF}
var
  FileInfo: TSearchRec;
  DosCode: Integer;
begin
  Result := DirectoryExists(path);
  if copy(path,length(path),1)<>'\' then
   path:=path+'\';
  if not Result then Exit;
  DosCode := FindFirst(Path + '*.*', faAnyFile, FileInfo);
  try
    while DosCode = 0 do begin
      if (FileInfo.Name[1] <> '.') and (FileInfo.Attr <> faVolumeID) then
      begin
        if (FileInfo.Attr and faDirectory = faDirectory) then
          Result := ClearDir(Path + FileInfo.Name, Delete) and Result
        else if (FileInfo.Attr and faVolumeID <> faVolumeID) then begin
          if (FileInfo.Attr and faReadOnly = faReadOnly) then
            FileSetAttr(Path + FileInfo.Name, faArchive);
          Result := DeleteFile(Path + FileInfo.Name) and Result;
        end;
      end;
      DosCode := FindNext(FileInfo);
    end;
  finally
    FindClose(FileInfo);
  end;
  if Delete and Result and (DosCode = FileNotFound) and
    not ((Length(Path) = 2) and (Path[2] = ':')) then
  begin
    RmDir(Path);
    Result := (IOResult = 0) and Result;
  end;
end;

解决方案 »

  1.   

    用WindosAPI:
    tmpSource := Source;
       tmpDriver := Driver;
       FillChar(FromBuf,Sizeof(FromBuf),0);
       FillChar(ToBuf,sizeof(ToBuf),0);
       StrPCopy(Frombuf,Source);
       StrPCopy(ToBuf,Driver);   With OpStRuc do
       begin
          Wnd := Handle;
          wFunc := FO_DELETE;
          pFrom := @FromBuf;
          pTo := @ToBuf;
          fFlags := FOF_MULTIDESTFILES  OR FOF_NOCONFIRMATION or FOF_CONFIRMMOUSE;
          fAnyOperationsAborted := False;
          hNameMappings := Nil;
          lpszProgressTitle := Nil;
       end;//end with;
       ShFileOperation(OpStruc);