function DeleteFolder(Path: string): Boolean; var search: TSearchRec; ret: integer; key: string; begin if Path[Length(Path)] <> '\' then Path:= Path+ '\'; 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 DeleteDirectory(NowPath + Search.name); end else begin if ((search.attr and fadirectory) <> fadirectory) then begin deletefile(NowPath + search.name); end; end; ret := FindNext(search); end; findClose(search); removedir(Path); result := True; end;
下面的代码将调用windows的删除过程uses ShellApi;function DelDirectory(const Path: string): boolean; var fo: TSHFILEOPSTRUCT; begin FillChar(fo, SizeOf(fo), 0); with fo do begin Wnd := 0; wFunc := FO_DELETE; pFrom := PChar(Path+ #0); pTo := #0#0; //fFlags := FOF_NOCONFIRMATION + FOF_SILENT;//加上这句,将不弹出对话框 end; Result := (SHFileOperation(fo) = 0); end;
procedure deletedirectory(dirname: string); var fo: TSHFILEOPSTRUCT; begin FillChar(fo, SizeOf(fo), 0); with fo do begin Wnd := 0; wFunc := FO_DELETE; pFrom := PChar(dirname+#0); fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR ; end; Result := (SHFileOperation(fo) = 0); end;
看来我RP有问题..奇怪了,前辈看下怎么回事,C:\klxq这个目录下的东西还在,不是只读的,还有faArchiv编译提示未声明,我是不是还漏了什么?procedure TForm1.Button4Click(Sender: TObject); begin if DeleteFolder('C:\klxq') then information('111'); end;function TForm1.DeleteFolder(Path: string): Boolean; var search: TSearchRec; ret: integer; key: string; begin if Path[Length(Path)]<>'\' then Path:=Path+'\'; key := Path+'*.*'; 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 //FileSetAttr(Path+Search.Name,faArchiv); DeleteFolder(Path+Search.Name); end else begin if ((Search.Attr and fadirectory)<> fadirectory) then //FileSetAttr(Path+Search.Name,faArchiv); DeleteFolder(Path+Search.Name); end; ret:=FindNext(Search); end; findClose(Search); Removedir(Path); result:=true; end;
function DeleteFolder(Path: string): Boolean;
var
search: TSearchRec;
ret: integer;
key: string;
begin
if Path[Length(Path)] <> '\' then
Path:= Path+ '\';
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
DeleteDirectory(NowPath + Search.name);
end else begin
if ((search.attr and fadirectory) <> fadirectory) then begin
deletefile(NowPath + search.name);
end;
end;
ret := FindNext(search);
end;
findClose(search);
removedir(Path);
result := True;
end;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(Path+ #0);
pTo := #0#0;
//fFlags := FOF_NOCONFIRMATION + FOF_SILENT;//加上这句,将不弹出对话框
end;
Result := (SHFileOperation(fo) = 0);
end;
procedure deletedirectory(dirname: string);
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(dirname+#0);
fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR ;
end;
Result := (SHFileOperation(fo) = 0);
end;
faanyfile:SysUtils
NowPath:换成Path
DeleteDirectory:换成DeleteFolder,这里有个递归调用
不好意思^_^
但是这个目录下的文件都还在,是怎么回事?
有一个问题要注意,有一些属性标记为只读的,就删除不掉,所以在查到文件时,把属性改一下
修改属性为非只读
FileSetAttr('文件名',faArchiv);
begin
if DeleteFolder('C:\klxq') then
information('111');
end;function TForm1.DeleteFolder(Path: string): Boolean;
var
search: TSearchRec;
ret: integer;
key: string;
begin
if Path[Length(Path)]<>'\' then
Path:=Path+'\';
key := Path+'*.*';
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
//FileSetAttr(Path+Search.Name,faArchiv);
DeleteFolder(Path+Search.Name);
end else
begin
if ((Search.Attr and fadirectory)<> fadirectory) then
//FileSetAttr(Path+Search.Name,faArchiv);
DeleteFolder(Path+Search.Name);
end;
ret:=FindNext(Search);
end;
findClose(Search);
Removedir(Path);
result:=true;
end;
pengderun
pengderun
试试2楼和3楼的代码吧,最近在外办公,回公司测试后结贴,谢谢各位的大侠相助
我想2楼和3楼的代码应该是我想要的吧