如何删除非空目录?(30分)

  • 主题发起人 主题发起人 sunylat
  • 开始时间 开始时间
S

sunylat

Unregistered / Unconfirmed
GUEST, unregistred user!
你好:
请问如何删除非空目录?
 
czzz (2002-1-22 12:27:00)
大家看看,这段为何不能删除非空目录:(代码是DFW是抄的!)
uses shellapi;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
T:TSHFileOpStruct;
P:String;
begin
P:='d:/address';//这里改成你要删除的任意目录名
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
pTo:=nil;
fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息
hNameMappings:=nil;
lpszProgressTitle:='正在删除文件夹';
fAnyOperationsAborted:=False;
End;
SHFileOperation(T);
end;

zw84611 (2002-1-22 12:34:00)
注意: P:='d:/address'+#0; //#0是字符串结束标志。

ydjjld (2002-1-22 13:04:00)
同意同意p是pchar类型不是string

czzz (2002-1-22 19:46:00)
多人接受答案了。

 
procedure TForm1.Button1Click(Sender: TObject);
var
myrec:TSHFILEOPSTRUCT ;
begin
With myrec DO Begin
Wnd:= Handle;
wFunc:= FO_DELETE;
pFrom:= 'c:/aaa';
pTo:='';
fFlags:= FOF_NOCONFIRMATION or FOF_FILESONLY;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
SHFileOperation(myrec);
end;
 
unit DelDir;

interface

uses windows,DelDirHelp;

function DeleteDir(sDirName: string): Boolean;

implementation



function DoRemoveDir(sDirName: string): Boolean;
var
hFindFile: THandle;//Cardinal;
tfile: string;
sCurDir: string;
bEmptyDir: Boolean;
FindFileData: WIN32_FIND_DATA;
begin
//Èç¹ûɾ³ýµÄÊÇ¿ÕĿ¼,ÔòÖÃbEmptyDirΪTrue
//³õʼʱ,bEmptyDirΪTrue
bEmptyDir := True;
//Ïȱ£´æµ±Ç°Ä¿Â¼
sCurDir := myGetCurrentDir;
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;
//ɾ³ýĿ¼µÄº¯Êý£ºDeleteDir

function DeleteDir(sDirName: string): Boolean;
begin
if Length(sDirName) <= 0 then
exit;
//ɾ³ý...
if (DoRemoveDir(sDirName) and myRemoveDir(sDirName)) then
Result := True
else
Result := False;
end;

end.


*************** http://infocaptor.3322.net/
 
多谢各位:
kingdeezj的比较工整,奉上微薄20分,请笑纳;
zw84611谢谢你,奉上微薄10分,请笑纳!
多谢各位帮忙!!!!
 
后退
顶部