delphi如何删除文件夹(有文件)

nani1233 2011-11-08 08:17:56
怎样删除一个里面有文件的文件夹 连文件一起删掉 求具体代码 谢谢
...全文
5004 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
山东蓝鸟贵薪 2014-03-31
  • 打赏
  • 举报
回复
感谢各位分享 上面的方法,我测试了,如果 当前有文件在使用的话,不没法 删除的 必须先 退出 所有程序后,才能清理所有文件及文件夹的
windbell_mq 2014-03-30
  • 打赏
  • 举报
回复
---- 2、删除目录 ---- 删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。 ---- 2.1删除目录的递归辅助函数:DoRemoveDir function DoRemoveDir(sDirName:String):Boolean; var hFindFile:Cardinal; tfile:String; sCurDir:String; bEmptyDir:Boolean; FindFileData:WIN32_FIND_DATA; begin //如果删除的是空目录,则置bEmptyDir为True //初始时,bEmptyDir为True bEmptyDir:=True; //先保存当前目录 sCurDir:=GetCurrentDir; SetLength(sCurDir,Length(sCurDir)); ChDir(sDirName); hFindFile:=FindFirstFile( '*.* ',FindFileData); if hFindFile < > INVALID_HANDLE_VALUE then begin repeat tfile:=FindFileData.cFileName; if (tfile= '. ') or (tfile= '.. ') then begin bEmptyDir:=bEmptyDir and True; Continue; end; //不是空目录,置bEmptyDir为False bEmptyDir:=False; if FindFileData.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY then begin if sDirName[Length(sDirName)] < > '\ ' then DoRemoveDir(sDirName+ '\ '+tfile) else DoRemoveDir(sDirName+tfile); if not RemoveDirectory(PChar(tfile)) then result:=false else result:=true; end else begin if not DeleteFile(PChar(tfile)) then result:=false else result:=true; end; until FindNextFile(hFindFile,FindFileData)=false; FindClose(hFindFile); end else begin ChDir(sCurDir); result:=false; exit; end; //如果是空目录,则删除该空目录 if bEmptyDir then begin //返回上一级目录 ChDir( '.. '); //删除空目录 RemoveDirectory(PChar(sDirName)); end; //回到原来的目录下 ChDir(sCurDir); result:=true; end; ---- 2.2删除目录的函数:DeleteDir function DeleteDir(sDirName:String):Boolean; begin if Length(sDirName) < =0 then exit; //删除... Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName); end;
hongss 2014-03-17
  • 打赏
  • 举报
回复
xbbtzhao01 2014-03-17
  • 打赏
  • 举报
回复
循环一下,看看是单个目录,还是多个级目录
mggsaca 2014-02-26
  • 打赏
  • 举报
回复
一个我用的函数,删除一个文件夹,参数丰富,功能强大, function DeleteTree(DeleteTreePath: string; IncludeCurrDir: boolean = true; DelFaileFileLst: TStrings = nil; DelSuccessFileLst: TStrings = nil): boolean; /// 删除一个目录及其下所有文件 function DeleteFileIntr(_STPath: string): boolean; var f: TSearchRec; // R: boolean; begin result := false; if Length(_stPath) = 0 then exit; if _StPath[length(_StPath)] <> '\' then _StPath := _stPath + '\'; Result := true; if FindFirst(_StPath + '*.*', faDirectory + faReadOnly + faHidden + faSysFile, f) = 0 then begin repeat if f.Attr and faDirectory > 0 then if (f.Name = '.') or (f.name = '..') then else begin if DeleteFileIntr(_StPath + f.name + '\') then if not RemoveDir(_StPath + f.name) then Result := false; end; until FindNext(f) <> 0; FindClose(f); end; if FindFirst(_StPath + '*.*', faArchive + faReadOnly + faHidden + faSysFile, f) = 0 then begin repeat if f.Attr and faDirectory = 0 then if (f.Name = '.') or (f.name = '..') then else begin if not DeleteFile(_StPath + f.name) then begin Result := false; if DelFaileFileLst <> nil then DelFaileFileLst.Add(_StPath + f.name + ' ...' + SysErrorMessage(GetlastError)); end else begin if DelSuccessFileLst <> nil then DelSuccessFileLst.Add(_StPath + f.name); end; end; until FindNext(f) <> 0; FindClose(f); end; end; begin // if DelFaileFileLst <> nil then DelFaileFileLst.Clear; // if DelSuccessFileLst <> nil then DelSuccessFileLst.Clear; Result := DeleteFileIntr(DeleteTreePath); if Length(DeleteTreePath) = 0 then exit; if IncludeCurrDir then begin if DeleteTreePath[length(DeleteTreePath)] = '\' then DeleteTreePath := Copy(DeleteTreePath, 1, Length(DeleteTreePath) - 1); if not RemoveDir(DeleteTreePath) then Result := false; end; end;
DelisPhi 2014-02-15
  • 打赏
  • 举报
回复
围观大神们的回答
山东蓝鸟贵薪 2014-02-15
  • 打赏
  • 举报
回复
学习学习
zhongguoren666 2011-11-10
  • 打赏
  • 举报
回复
同意三楼!
SQLDebug_Fan 2011-11-09
  • 打赏
  • 举报
回复
用Shell删除的函数,供你参考:

function DeleteDirectory(const DirName: string; const UI: Boolean = False): Boolean;
{
删除目录
}
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := GetActiveWindow;
wFunc := FO_DELETE;
pFrom := PChar(DirName + #0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION + FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;

function ClearDirectory(const DirName: string; const IncludeSub, ToRecyle: Boolean): Boolean;
{
清除目录
}
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := GetActiveWindow;
wFunc := FO_DELETE;
pFrom := PChar(DirName + '\*.*' + #0);
pTo := #0#0;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI
or (Ord(not IncludeSub) * FOF_FILESONLY)
or (ORd(ToRecyle) or FOF_ALLOWUNDO);
end;
Result := (SHFileOperation(fo) = 0);
end;
ADSLAN 2011-11-09
  • 打赏
  • 举报
回复
2010中的用法 更简单

uses
IOUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
TDirectory.Delete('D:\TDDownload\fa', true);
end;
true时可以删除非空文件夹
一剑飘雪 2011-11-09
  • 打赏
  • 举报
回复

function DelDirectory(const Source:string): Boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
22222bbb 2011-11-08
  • 打赏
  • 举报
回复
RemoveDir

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧