uses FileCtrl; procedure DelTree(const Directory: TFileName);
var
DrivesPathsBuff: array[0..1024] of char;
DrivesPaths: string;
len: longword;
ShortPath: array[0..MAX_PATH] of char;
dir: TFileName;
procedure rDelTree(const Directory: TFileName);
var
SearchRec: TSearchRec;
Attributes: LongWord;
ShortName, FullName: TFileName;
pname: pchar;
begin
if FindFirst(Directory + '*', faAnyFile and not faVolumeID,
SearchRec) = 0 then begin
try
repeat // 检测所有的文件和目录
if SearchRec.FindData.cAlternateFileName[0] = #0 then
ShortName := SearchRec.Name
else
ShortName := SearchRec.FindData.cAlternateFileName;
FullName := Directory + ShortName;
if (SearchRec.Attr and faDirectory) <> 0 then begin
// 是一个目录
if (ShortName <> '.') and (ShortName <> '..') then
rDelTree(FullName + '\');
end else begin
// 是一个文件
pname := PChar(FullName);
Attributes := GetFileAttributes(pname);
if Attributes = $FFFFFFFF then
raise EInOutError.Create(SysErrorMessage(GetLastError));
if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
SetFileAttributes(pname, Attributes and not
FILE_ATTRIBUTE_READONLY);
if Windows.DeleteFile(pname) = False then
raise EInOutError.Create(SysErrorMessage(GetLastError));
end;
until FindNext(SearchRec) <> 0;
except
FindClose(SearchRec);
raise;
end;
FindClose(SearchRec);
end;
if Pos(#0 + Directory + #0, DrivesPaths) = 0 then begin
// 如果不是根目录,就删除
pname := PChar(Directory);
Attributes := GetFileAttributes(pname);
if Attributes = $FFFFFFFF then
raise EInOutError.Create(SysErrorMessage(GetLastError));
if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
SetFileAttributes(pname, Attributes and not
FILE_ATTRIBUTE_READONLY);
if Windows.RemoveDirectory(pname) = False then begin
raise EInOutError.Create(SysErrorMessage(GetLastError));
end;
end;
end;
// ----------------
begin
DrivesPathsBuff[0] := #0;
len := GetLogicalDriveStrings(1022, @DrivesPathsBuff[1]);
if len = 0 then
raise EInOutError.Create(SysErrorMessage(GetLastError));
SetString(DrivesPaths, DrivesPathsBuff, len + 1);
DrivesPaths := Uppercase(DrivesPaths);
len := GetShortPathName(PChar(Directory), ShortPath, MAX_PATH);
if len = 0 then
raise EInOutError.Create(SysErrorMessage(GetLastError));
SetString(dir, ShortPath, len);
dir := Uppercase(dir);
rDelTree(IncludeTrailingBackslash(dir));
end;
Sample calls
------------ 删除 C:\TEMP\A123 目录
DelTree('C:\TEMP\A123'); 使用方法: DelTree('A:'); // or DelTree('A:\');
var
DrivesPathsBuff: array[0..1024] of char;
DrivesPaths: string;
len: longword;
ShortPath: array[0..MAX_PATH] of char;
dir: TFileName;
procedure rDelTree(const Directory: TFileName);
var
SearchRec: TSearchRec;
Attributes: LongWord;
ShortName, FullName: TFileName;
pname: pchar;
begin
if FindFirst(Directory + '*', faAnyFile and not faVolumeID,
SearchRec) = 0 then begin
try
repeat // 检测所有的文件和目录
if SearchRec.FindData.cAlternateFileName[0] = #0 then
ShortName := SearchRec.Name
else
ShortName := SearchRec.FindData.cAlternateFileName;
FullName := Directory + ShortName;
if (SearchRec.Attr and faDirectory) <> 0 then begin
// 是一个目录
if (ShortName <> '.') and (ShortName <> '..') then
rDelTree(FullName + '\');
end else begin
// 是一个文件
pname := PChar(FullName);
Attributes := GetFileAttributes(pname);
if Attributes = $FFFFFFFF then
raise EInOutError.Create(SysErrorMessage(GetLastError));
if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
SetFileAttributes(pname, Attributes and not
FILE_ATTRIBUTE_READONLY);
if Windows.DeleteFile(pname) = False then
raise EInOutError.Create(SysErrorMessage(GetLastError));
end;
until FindNext(SearchRec) <> 0;
except
FindClose(SearchRec);
raise;
end;
FindClose(SearchRec);
end;
if Pos(#0 + Directory + #0, DrivesPaths) = 0 then begin
// 如果不是根目录,就删除
pname := PChar(Directory);
Attributes := GetFileAttributes(pname);
if Attributes = $FFFFFFFF then
raise EInOutError.Create(SysErrorMessage(GetLastError));
if (Attributes and FILE_ATTRIBUTE_READONLY) <> 0 then
SetFileAttributes(pname, Attributes and not
FILE_ATTRIBUTE_READONLY);
if Windows.RemoveDirectory(pname) = False then begin
raise EInOutError.Create(SysErrorMessage(GetLastError));
end;
end;
end;
// ----------------
begin
DrivesPathsBuff[0] := #0;
len := GetLogicalDriveStrings(1022, @DrivesPathsBuff[1]);
if len = 0 then
raise EInOutError.Create(SysErrorMessage(GetLastError));
SetString(DrivesPaths, DrivesPathsBuff, len + 1);
DrivesPaths := Uppercase(DrivesPaths);
len := GetShortPathName(PChar(Directory), ShortPath, MAX_PATH);
if len = 0 then
raise EInOutError.Create(SysErrorMessage(GetLastError));
SetString(dir, ShortPath, len);
dir := Uppercase(dir);
rDelTree(IncludeTrailingBackslash(dir));
end;
Sample calls
------------ 删除 C:\TEMP\A123 目录
DelTree('C:\TEMP\A123'); 使用方法: DelTree('A:'); // or DelTree('A:\');
解决方案 »
- 两个不同的dll,有个函数名是一样,参数不一样,两个函数都要用,程序怎么处理?
- TDbgrid的问题
- 请 progress99(如履薄冰)进
- 请问各位高手一个问题。!!!
- 在DELPHI中执行SQL2000自带的一个存储过程(sp_pkeys),用storedproc该怎么执行?急,急,急
- 我做了个邮件收发的软件,收取邮件的将邮件保存到数据库中(包括附件),然后想在将数据库中的附件保存到文件中,不知道怎么做好!
- 在ActiveForm中在显示另一个form时报错!该如何解决????
- delphi与NT
- SQl小问题。
- 如何让EXE文件执行后返回一个整数值给WINDOWS?
- 谁有WINDOWS API FOR DELPHI的中文手册要有例程!!!!!!!
- 请各位高手一定要帮我一下,我现在是试用关键时刻!
var
Sr: TSearchRec;
PathName: string;
k: Integer;
begin
PathName := DirName + '\*.*';
k := FindFirst(PathName, faAnyFile, Sr);
while k = 0 do
begin
if (Sr.Attr in [16, 16+1, 16+2, 16+4, 16+5, 16+32]) and (Pos(Sr.Name, '..') = 0) then
begin
Sr.Attr := 16;
DelPath(DirName + '\' + Sr.Name);
end
else
if Pos(Sr.Name, '..') = 0 then
begin
Sr.attr := 0;
DeleteFile(PChar(DirName + '\' + Sr.Name));
end;
k := FindNext(Sr);
end;
RmDir(DirName);
end;
var
SHFileOpStruct: TSHFileOpStruct;
begin
with SHFileOpStruct do
begin
Wnd := AHandle;
wFunc := FO_DELETE;
pFrom := PChar(AFileName);
fFlags := FOF_ALLOWUNDO;
end;
SHFileOperation(SHFileOpStruct);
end;
uses ShellApi;function DoDelTree(mTreeName: string): Boolean;
begin
Result := ShellExecute(Application, 'Open',
PChar(PChar('deltree')), PChar(Format('%s', [mTreeName])), nil, SW_SHOW) = 0;
end;
begin
Result := ShellExecute(Application.Handle, 'Open',
PChar(PChar('deltree')), PChar(Format('%s', [mTreeName])), nil, SW_SHOW) = 0;
end;//如果win系统没有deltree.com就将deltree.old 改为deltree.com