Home > Back-end >  Delphi how to delete folders (file)
Delphi how to delete folders (file)

Time:10-04

How to delete a folder with files inside the file delete for specific code thank you together

CodePudding user response:

RemoveDir

CodePudding user response:

With Shell delete function, for your reference:
 
The function DeleteDirectory (const DirName: string; Const UI: Boolean=False) : Boolean;
{
Delete the directory
}
Var
Fo: TSHFILEOPSTRUCT;
The begin
FillChar (fo, SizeOf (fo), 0).
With fo do
The begin
Wnd:=GetActiveWindow;
WFunc:=FO_DELETE;
PFrom:=PChar (DirName + # 0);
Office:=# 0 # 0;
FFlags:=FOF_NOCONFIRMATION + FOF_SILENT;
end;
Result:=(SHFileOperation (fo)=0).
end;

The function ClearDirectory (const DirName: string; Const IncludeSub ToRecyle: Boolean) : Boolean;
{
Remove directory
}
Var
Fo: TSHFILEOPSTRUCT;
The begin
FillChar (fo, SizeOf (fo), 0).
With fo do
The begin
Wnd:=GetActiveWindow;
WFunc:=FO_DELETE;
PFrom:=PChar (DirName + '\ *. *' + # 0);
Office:=# 0 # 0;
FFlags:=FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI
Or (word (not IncludeSub) * FOF_FILESONLY)
Or (word (ToRecyle) or FOF_ALLOWUNDO);
end;
Result:=(SHFileOperation (fo)=0).
end;

CodePudding user response:

 
The function DelDirectory (const Source: string) : Boolean;
Var
Fo: TSHFILEOPSTRUCT;
The begin
FillChar (fo, SizeOf (fo), 0).
With fo do
The begin
Wnd:=0;
WFunc:=FO_DELETE;
PFrom:=PChar (source + # 0);
Office:=# 0 # 0;
FFlags:=FOF_NOCONFIRMATION + FOF_SILENT;
end;
Result:=(SHFileOperation (fo)=0).
end;

CodePudding user response:

The usage of the 2010 more simple

USES the
IOUtils;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
TDirectory. Delete (' D: \ TDDownload \ fa ', true);
end;
True can delete is not empty folder

CodePudding user response:

Agree on the third floor!

CodePudding user response:

Learning how to learn

CodePudding user response:

looked great god's answer

CodePudding user response:

I use a function, delete a folder, parameter is rich, powerful,
The function DeleteTree (DeleteTreePath: string; IncludeCurrDir: Boolean=true; DelFaileFileLst: TStrings=nil; DelSuccessFileLst: TStrings=nil) : Boolean;///to remove a directory and all files under the

The function DeleteFileIntr (_STPath: string) : Boolean;
Var
F: TSearchRec;
//R: Boolean;
The begin
Result:=false;
If Length (_stPath)=0 then the exit;
If _StPath [length (_StPath)] <> '\' then
_StPath:=_StPath + '\';
Result:=true;
If FindFirst (_StPath + '*. *, faDirectory + + faHidden faReadOnly + faSysFile, f)=0 then
The begin
Repeat
If f.A TTR event and faDirectory & gt; 0 then
If (ame=') or (ame='.. ') then
The else
The begin
If DeleteFileIntr (_StPath + motor ame + '\') then
If not RemoveDir (_StPath + motor ame) then
Result:=false;
end;
Until FindNext (f) & lt;> 0;
FindClose (f);
end;
If FindFirst (_StPath + '*. *, faArchive + + faHidden faReadOnly + faSysFile, f)=0 then
The begin
Repeat
If f.A TTR event and faDirectory=0 then
If (ame=') or (ame='.. ') then
The else
The begin
If not DeleteFile (_StPath + motor ame) then
The begin
Result:=false;
If DelFaileFileLst & lt;> Nil then DelFaileFileLst. Add (_StPath + motor ame + '... '+ SysErrorMessage GetlastError ());
End the else
The begin
If DelSuccessFileLst & lt;> Nil then DelSuccessFileLst. Add (_StPath + motor ame);
end;
end;
Until FindNext (f) & lt;> 0;
FindClose (f);
end;
end;
The begin
//if DelFaileFileLst & lt;> Nil then DelFaileFileLst. The Clear;
//if DelSuccessFileLst & lt;> Nil then DelSuccessFileLst. The Clear;
Result:=DeleteFileIntr (DeleteTreePath);
If Length (DeleteTreePath)=0 then the exit;
If IncludeCurrDir then
The begin
If DeleteTreePath [length (DeleteTreePath)]='\' then
DeleteTreePath:=Copy (DeleteTreePath, 1, Length (DeleteTreePath) - 1);
If not RemoveDir (DeleteTreePath) then
Result:=false;
end;
end;

CodePudding user response:

Cycle, see is a single directory, or multiple level directory

CodePudding user response:

CodePudding user response:

- 2, delete directory

- delete the directory and copy the directory is very similar, but in order to delete an empty directory in the root directory, need to set a flag variable in the auxiliary function, that is: if you remove the directory is empty, then buy bEmptyDir is True, it has been expressed in the dark box,

- delete the directory recursion auxiliary function: 2.1 DoRemoveDir

The function DoRemoveDir (sDirName: String) : Boolean;
Var
HFindFile: Cardinal;
Tfile: String;
SCurDir: String;
BEmptyDir: Boolean;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related