Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Poss. to delete several files in one go?

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi All

I'd like to completely empty a directory and was wondering if there's a quick and easy solution. I tried the following but it access violates on the SHFileOperation:-

procedure TFormMain.DeleteFilesInDir;
var FileOpInfo : TSHFileOpStruct;
begin
with FileOpInfo do
begin
wnd := self.Handle;
wFunc := FO_DELETE;
if edCsvDirAct.Caption[length(edCsvDirAct.Caption)] <> '\' then
edCsvDirAct.Caption := edCsvDirAct.Caption +'\';
pFrom := pChar(edCsvDirAct.Caption+'*.*');
fFlags := FOF_FILESONLY;//FOF_ALLOWUNDO;
end;

SHFileOperation(FileOpInfo);
SHFreeNameMappings(GlobalHandle(FileOpInfo.hNameMappings));
end;

A straight delete of the directory would be good but RemoveDirectory needs an empty dir.

Will I have to do it with a FindFirst and DeleteFile? Was hoping for a more subtle solution...

lou
 
Hey lou - I don't know if you want to use this method, but here's what I did. I used the Win3.1 component FileListBox, but I don't even know if this is the best way to do this - just pass the directory as a string to DirName. Hope this helps.


var I, J :Integer;
var FName, DirName : string;
begin
FileListBox.ApplyFilePath(DirName);
J := FileListBox.Items.Count;
For I := 0 to J - 1 do
Begin
FName := FileListBox.Items.Strings;
DeleteFile(DirName + '\' +FName);
end;

RmDir(DirName);
end;
 
My reply kinda got messed up in two parts:

there is supposed to be brackets after the Items.Strings in the FName := line, and the backslash in the deletefile line

it should be:

FName := FileListBox.Items.StringsOPENBRACKET I CLOSEDBRACKET;

DeleteFile(DirName + BackSlash + FName);

hope that makes sense
 
Hi,
I think that you are using function SHFileOperation incorrectly. What I want to emphisize is, you must put two &quot;chr($0)&quot; in the end of the parameter &quot;pFrom&quot;, try this:

function OperateFileDelete(aFrom:String):Boolean;
var
s:TShFileOpStruct;
fb:Array [0..256] of char;
i:Integer;
p1:pchar;
begin
Result:=False;

if Length(aFrom) in [1..255] then
begin
p1:=@fb[0];
for i:=1 to Length(aFrom) do
begin
p1^:=aFrom;
Inc(p1);
end;
p1^:=Chr($0);
Inc(p1);
p1^:=Chr($0);

with s do
begin
Wnd:=Application.Handle;
wFunc:=fo_Delete;
pFrom:=@fb[0];
pTo:=nil;
fFlags:=Fof_AllowUndo or Fof_NoConfirmation
or Fof_NoConfirmMkDir or Fof_Silent;
hNameMappings:=nil;
lpszProgressTitle:=nil;
end;
if ShfileOperation(s)=0 then
Result:=True;
end;
end;

Hope this helps.
 
That'll be

if Length(aFrom) in [1..255] then
begin
p1:=@fb[0];
for i:=1 to Length(aFrom) do
begin
p1^:=aFrom;
 
It seems to me that the findfirst is easier than this. Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
hi, I'm back! been on holiday, still am for that matter.

Many thanks for all the replies.

..bbegley, I have done it that way now, using FindFirst.

Thanks again
lou
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top