PS: SHFileOperation() is very powerful, you might want to take a look at the Flags (fFlags) to adjust it to your needs (like showing an animation, ...)
Markus
Main Topics
Browse All TopicsOkay, how can I delete a directory of files? I would like to delete more than one directory also. :)
I would like to know how to do it like... (far fetched example just so you understand me)
-----------------
procedure TForm1.Button1Click(Sender
begin
DeleteDirectory('edit1.tex
DeleteDirectory('edit2.tex
end;
-----------------
lol, you know what I mean? That is just a made up example of how I would like it to be. So the User can specify which directory to delete. I would LOVE if someone could help me out on this one! Thank you very much.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
procedure DelTree(Directory: string);
var
fo: SHFILEOPSTRUCT;
pDir: array[0..255] of char;
begin
fillChar(pDir, 256, #0);
strPCopy(pDir, Directory);
fillchar(fo,sizeof(fo),0);
fo.Wnd := application.Handle;
fo.wFunc := FO_DELETE;
fo.pFrom := pDir;
fo.pTo := '';
fo.fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
SHFileOperation(fo);
end;
I would suggest looking at the Project JEDI Code Library (JCL). http://sourceforge.net/pro
Unit JclFileUtils;
Has two functions:
DelTree
DelTreeEx
These functions allow you to Delete a directory and all its sub-directories without relying on Windows Explorer Shell to do the work for you by using SHFileOperation. SHFileOperation is really not much different than you hilighting the directory and pressing delete. By using the flags the others have mentioned it just supresses and hides the UI windows from popping up.
I would venture to guess this method is also definately much more portable. This actually does the work of determining what files and folders are in each directory and deleting them all recursively using lower level functions. DelTreeEx adds the ability to report progress so you can show your own progress indicator of some kind.
Mike
Normally you can't delete a directory that is not empty...
If you want to empty a directory 'C:\Windows\TempX' you should do this:
Procedure ShellDirDelete(Folder: String);
var
FileOpRec : TSHFileOpStruct;
begin
with FileOpRec do
begin
Wnd := Handle ;
wFunc := FO_DELETE;
if Folder[Length(Folder)] <> '\' then
pFrom := PChar(Folder+'\*.*'#0#0)
else
pFrom := PChar(Folder+'*.*'#0#0);
pTo := nil ;
fFlags := FOF_NOCONFIRMATION or FOF_ALLOWUNDO or FOF_FILESONLY;
hNameMappings := nil;
lpszProgressTitle := nil;
end;
if (SHFileOperation(FileOpRec
(FileOpRec.fAnyOperationsA
begin
ShowMessage('Copy Operation was canceled');
end;
end;
begin
ShellDirDelete('C:\Windows
end;
hmmm if thi is stil open i will do the real option;
s1 is string list or memo (if memo only 32 kb is the limit for names)
procedure deletedir(s:string);
begin
s1.items.clear;
deldir(s);
for i:=s1.itemindex-1 downto 1 do
deletefile(s1[i]);
end;
procedure tform1.deldir(s:string);
var rec :tsearchrec;
begin
if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then
begin
repeat
if (rec.name<>'.') and (rec.name<>'..') then
begin
s1.items.Add(s+Rec.Name);
if rec.attr=fadirectory then getalldir(s+rec.name+'\');
end;
until SysUtils.FindNext(Rec) <> 0;
sysutils.FindClose(rec);
end;
great code;
dys7opia:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
to split points between DaFox, akaSurreal, calinutz and ILE
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
If you will get a useful answer to your newer question next time, please accept it after evaluating. By this way the experts will get sooner his expert points for supporting you. If you have question about, please look at http://www.experts-exchang
kacor
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: DaFoxPosted on 2003-04-28 at 11:46:52ID: 8414601
Hi dys7opia!
s;
Try this little procedure:
procedure DeleteDir(aDir: String);
var
T: TSHFileOpStruct;
begin
Fillchar(T, SizeOf(T), #0);
with T do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(aDir + #0#0);
fFlags := FOF_ALLOWUNDO or FOF_SILENT or FOF_NOCONFIRMATION;
end;
Application.ProcessMessage
if (SHFileOperation(T) <> 0) then RemoveDir(aDir);
end;
Markus