Delphi中如何删除当前目录下的一个指定的文件夹
发布网友
发布时间:2022-05-13 17:58
我来回答
共2个回答
热心网友
时间:2023-10-19 08:25
o(∩_∩)o...,同上就对了
要是不想删除到回收站了,就用下面的代码:
qTempFolder传路径,似乎要引用ShellAPI单元吧(忘记了)
function DeleteTempFolder(qTempFolder: PChar): Integer;
var
SR: TSearchRec;
FR: Integer;
begin
Result := 0;
if not DirectoryExists(qTempFolder) then
Exit;
try
if Copy(qTempFolder, Length(qTempFolder), 1) <> '\' then
qTempFolder := PChar(qTempFolder + '\');
FR := FindFirst(qTempFolder + '*.*', FaAnyfile, SR);
while FR = 0 do
begin
if ((SR.Attr and FaDirectory) = FaDirectory) and (SR.Name <> '.') and
(SR.Name <> '..') then
if DeleteTempFolder(PChar(qTempFolder + SR.Name)) = -1 then
Break;
if ((SR.Attr and FaDirectory <> FaDirectory) and (SR.Attr and FaVolumeID
<> FaVolumeID)) then
begin
SysUtils.FileSetAttr(qTempFolder + SR.Name,
SysUtils.FileGetAttr(qTempFolder + SR.Name) and (not
SysUtils.faReadOnly)); //取消文件的只读属性
if not DeleteFile(PChar(qTempFolder + SR.Name)) then
Break;
end;
FR := FindNext(SR);
end;
SysUtils.FindClose(SR);
RemoveDirectory(qTempFolder);
except
Result := -1;
end;
end;
热心网友
时间:2023-10-19 08:26
多给点分啊,delpath传你要删除的文件夹路径,子文件夹会一并删除。
procere Tfrm_PaperIn.DelTEMPFiles(delpath: String);
Var
T:TSHFileOpStruct;
P:String;
begin
P:= delpath;//要删除的任意目录名
With T do
Begin
Wnd:= 0;
wFunc:= FO_DELETE;
pFrom:= Pchar(P);
pTo:= nil;
fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息
hNameMappings:= nil;
fAnyOperationsAborted:= False;
End;
SHFileOperation(T);
end;