转:delphi 删除指定文件夹下所有文件

function TFtpContentThd.DeleteDirectory(NowPath: string): Boolean;
var
  search: TSearchRec;
  ret: integer;
  key: string;
begin
  if NowPath[Length(NowPath)] <> '\' then
    NowPath := NowPath + '\';
  key := NowPath + '*.*';
  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
      begin
        DeleteDirectory(NowPath + search.name);
        removedir(NowPath + search.name); //如果需要删除文件夹则添加
      end;
    end
    else
    begin
      if ((search.Attr and fadirectory) <> fadirectory) then
      begin
        deletefile(PAnsiChar(NowPath + search.name));

      end;
    end;
    ret := FindNext(search);
  end;
  FindClose(search.FindHandle);
  //FindClose(search); //发现在线程里面不能用这,只能用上面的句柄
  //removedir(NowPath); //如果需要删除最外层文件夹则添加
  result := True;
end;

  

posted @ 2015-04-03 11:14  海蓝7  阅读(2078)  评论(0编辑  收藏  举报