PeterdeB
asked on
How to empty a directory leaving one file?
Hi my dear friends!
This is what I have and this works great:
************************** ********** ********** ********** ********** ********** ***
function KillFiles(Path: string): Boolean;
var
Found: Integer;
Attr: Integer;
SearchRec: TSearchRec;
FileName: string;
begin
Attr := faAnyFile;
Found := FindFirst(Path + '\*.*', Attr, SearchRec);
while (Found=0) do
try
if ((IntToHex(SearchRec.Attr, 8)='00000810')
or (IntToHex(SearchRec.Attr, 8)='00000010'))
and (not (SearchRec.Name[1]='.')) then
begin
FileName := Path + '\' + SearchRec.Name;
RemoveDir(FileName);
KillFiles(FileName);
end;
if (not ((SearchRec.Attr=faDirecto ry) or (SearchRec.Attr=17))
and not (SearchRec.Name='.') and not (SearchRec.Name='..')) then
begin
FileName := Path + '\' + SearchRec.Name;
if ((FileGetAttr(FileName) and faReadOnly) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
if ((FileGetAttr(FileName) and faHidden) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faHidden);
if ((FileGetAttr(FileName) and faSysFile) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faSysFile);
DeleteFile(FileName);
KillFiles(Path);
end;
Found := FindNext(SearchRec); //get next file in directory
except
end;
FindClose(SearchRec);
end;
procedure TForm1.btnDeleteClick(Send er: TObject);
var i : cardinal;
user:string;
begin
i:=255;
//ExtractFilePath('c:\docu ments and settings\');
setlength(user,i);
getusername(pchar(user),i) ;
setlength(user,(i-1));
//ShowMessage(user);
if not
killfiles('c:\documents and settings\'+user+'\local settings\temporary internet files\content.ie5')
then ShowMessage('failed!');
end;
end.
************************** ********** ********** ********** ********** *********
Thanks to esoftbg and inthe btw :)
Now I want this program to leave one file unharmed untouched.....the index.dat....
How should I implement this? I'd say there would be other ways of emptying the cache of IE but so far this is my solution any other solutions are welcome as well ofcourse!
Best regards,
Paul
Ps working samples always do the trick ofcourse!
This is what I have and this works great:
**************************
function KillFiles(Path: string): Boolean;
var
Found: Integer;
Attr: Integer;
SearchRec: TSearchRec;
FileName: string;
begin
Attr := faAnyFile;
Found := FindFirst(Path + '\*.*', Attr, SearchRec);
while (Found=0) do
try
if ((IntToHex(SearchRec.Attr,
or (IntToHex(SearchRec.Attr, 8)='00000010'))
and (not (SearchRec.Name[1]='.')) then
begin
FileName := Path + '\' + SearchRec.Name;
RemoveDir(FileName);
KillFiles(FileName);
end;
if (not ((SearchRec.Attr=faDirecto
and not (SearchRec.Name='.') and not (SearchRec.Name='..')) then
begin
FileName := Path + '\' + SearchRec.Name;
if ((FileGetAttr(FileName) and faReadOnly) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);
if ((FileGetAttr(FileName) and faHidden) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faHidden);
if ((FileGetAttr(FileName) and faSysFile) > 0) then
FileSetAttr(FileName, FileGetAttr(FileName) xor faSysFile);
DeleteFile(FileName);
KillFiles(Path);
end;
Found := FindNext(SearchRec); //get next file in directory
except
end;
FindClose(SearchRec);
end;
procedure TForm1.btnDeleteClick(Send
var i : cardinal;
user:string;
begin
i:=255;
//ExtractFilePath('c:\docu
setlength(user,i);
getusername(pchar(user),i)
setlength(user,(i-1));
//ShowMessage(user);
if not
killfiles('c:\documents and settings\'+user+'\local settings\temporary internet files\content.ie5')
then ShowMessage('failed!');
end;
end.
**************************
Thanks to esoftbg and inthe btw :)
Now I want this program to leave one file unharmed untouched.....the index.dat....
How should I implement this? I'd say there would be other ways of emptying the cache of IE but so far this is my solution any other solutions are welcome as well ofcourse!
Best regards,
Paul
Ps working samples always do the trick ofcourse!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Regards Paul :)