删除目录,先删除目录下的文件:procedure RemoveNotNullDir(pPath: string); var search: TSearchRec; ret: integer; key: string; begin if pPath[Length(pPath)] <> '\' then pPath := pPath + '\'; key := pPath + '*.*'; 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 RemoveNotNullDir(pPath + Search.name); end else begin if ((search.attr and fadirectory) <> fadirectory) then begin deletefile(pPath + search.name); end; end; ret := FindNext(search); end; findClose(search); removedir(pPath); end;
uses ShellAPI;var
a: TSHFileOpStruct;
begin
FillChar(a, SizeOf(a), 0);
a.Wnd := Handle;
a.wFunc := FO_DELETE;
a.pFrom := 'c:\1.txt' + #0;
a.fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
ShellAPI.SHFileOperation(a);
end;
想偷懒的话找黑防08年9期的源代码(他的网站上有),《也谈Delphi下重启移动删除机制》那篇文章的源码
将文件设成faAnyFile,即可删除(参加http://blog.sina.com.cn/s/blog_43c078170100admv.html)。
对于文件则先循环删除里面的文件再删除文件夹。
DeleteFile(your file's name); //删除文件
var
search: TSearchRec;
ret: integer;
key: string;
begin
if pPath[Length(pPath)] <> '\' then
pPath := pPath + '\';
key := pPath + '*.*';
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
RemoveNotNullDir(pPath + Search.name);
end else begin
if ((search.attr and fadirectory) <> fadirectory) then begin
deletefile(pPath + search.name);
end;
end;
ret := FindNext(search);
end;
findClose(search);
removedir(pPath);
end;