PeterdeB
asked on
How to modify this DeleteExclude Function?
Hi Folks,
I'm using this code to clean up some folders >
function TForm1.DeleteExclude(Path: String; ExcludeFile: String; dispStr: String; lbMain: TRZListbox): Boolean;
var listFiles: TStringList;
srFind: TSearchRec;
szPath: String;
dwSize: LongWord;
dwFiles: Integer;
dwFind: Integer;
begin
// Clear output
//Output.Clear;
// Make sure path is correct
szPath:=ExcludeTrailingBac kSlash(Pat h);
// Init the find
dwFind:=FindFirst(szPath+' \*.*', faAnyFile, srFind);
// Create list to hold files and paths
listFiles:=TStringList.Cre ate;
// Resource protection
try
// Add progress
lbMain.AddObject(Format('[ %s]'+dispS tr,[TimeTo Str(Now)]) ,Image1.Pi cture.Bitm ap);
lbMain.AddObject(Format('[ %s] Folders en bestanden worden geteld...',[TimeToStr(Now) ]),Image1. Picture.Bi tmap);
// Check find
if (dwFind = 0) then
begin
// Resource protection
try
// While sucess
while (dwFind = 0) do
begin
// Check name
if ((Length(srFind.Name) > 0) and not(srFind.Name[1] = '.')) then
begin
// Check path
if ((srFind.Attr and faDirectory) = faDirectory) then
// Recurse all files in folder
RecursePath(szPath+'\'+srF ind.Name, listFiles)
// Compare against exclusion
else if (CompareText(srFind.Name, ExtractFileName(ExcludeFil e)) <> 0) then
// Add to list with file size
listFiles.AddObject(szPath +'\'+srFin d.Name, Pointer(srFind.Size));
end;
// Find next
dwFind:=FindNext(srFind);
end;
finally
// Close the find
FindClose(srFind);
end;
end;
// Set default result
result:=True;
// Check the list
if (listFiles.Count > 0) then
begin
// Set size and file count
dwFiles:=0;
dwSize:=0;
// Display file and directory count
for dwFind:=0 to Pred(listFiles.Count) do
begin
// Check for file
if (Integer(listFiles.Objects [dwFind]) >= 0) then
begin
// Inc file count
Inc(dwFiles);
// Increment the total size
Inc(dwSize, Integer(listFiles.Objects[ dwFind]));
end;
end;
// Add progress
lbMain.AddObject(Format('[ %s] Aantal folders: %d', [TimeToStr(Now), listFiles.Count-dwFiles]), Image1.Pic ture.Bitma p);
lbMain.AddObject(Format('[ %s] Aantal bestanden: %d', [TimeToStr(Now), dwFiles]),Image1.Picture.B itmap);
lbMain.AddObject(Format('[ %s] In beslag genomen ruimte: %s', [TimeToStr(Now), FormatFileSize(dwSize)]),I mage1.Pict ure.Bitmap );
lbMain.AddObject(Format('[ %s] Folders en bestanden worden verwijderd!', [TimeToStr(Now)]),Image1.P icture.Bit map);
// Allow updates
Application.ProcessMessage s;
// Reset size again
dwSize:=0;
// Now delete the file / paths
for dwFind:=Pred(listFiles.Cou nt) downto 0 do
begin
// Check for path
if (Integer(listFiles.Objects [dwFind]) = -1) then
// Remove path
result:=RemoveDirectory(PC har(listFi les[dwFind ]))
else
begin
// Set file attr
SetFileAttributes(PChar(li stFiles[dw Find]), FILE_ATTRIBUTE_ARCHIVE);
// Attempt to remove
result:=DeleteFile(PChar(l istFiles[d wFind]));
// Check delete
if result then Inc(dwSize, Integer(listFiles.Objects[ dwFind]));
end;
// Break on failure
if not(result) then break;
end;
// Check result
if not(result) then
lbMain.AddObject(Format('[ %s] Er is iets fout gegaan tijdens het verwijderen!', [TimeToStr(Now)]), image3.Picture.bitmap);
lbMain.AddObject(Format('[ %s] Vrij gemaakte schijfruimte: %s', [TimeToStr(Now), FormatFileSize(dwSize)]), image1.picture.bitmap);
lbMain.AddObject('',Image2 .Picture.B itmap);
end
else
// Add progress
lbMain.AddObject(Format('[ %s] Niks te verwijderen!', [TimeToStr(Now)]), image1.Picture.Bitmap);
lbMain.AddObject('',Image2 .Picture.B itmap);
finally
// Free the list
listFiles.Free;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
var dirStr: string;
begin
DeleteExclude(GetShellFold er('Cache' )+ '\Content.IE5','index.dat' , ' Internet Explorer tijdelijke bestanden', lbMain);
DeleteExclude(GetShellFold er('histor y')+ '\History.IE5','index.dat' , ' Internet Explorer Geschiedenis', lbMain);
DeleteExclude(GetShellFold er('local settings')+ '\Temp', 'index.dat', ' Tijdelijke bestanden', lbMain);
DeleteExclude(GetShellFold er('Recent '), 'index.dat', ' Onlangs geopende bestanden', lbMain );
DeleteExclude(GetShellFold er('Cookie s'), 'index.dat', ' Cookies', lbMain );
DeleteExclude(GetFolder(di rStr), 'index.dat', ' FireFox tijdelijke bestanden', lbMain);
end;
function TForm1.GetShellFolder (const folder: string):string;
const cWin32 = 'SOFTWARE\Microsoft\Window s\CurrentV ersion';
cSHellPrefix = '\Explorer\Shell Folders';
var r: TRegistry;
begin
Result:= '';
r:= TRegistry.Create;
try
r.Rootkey:=HKEY_CURRENT_US ER;
if r.OpenKey (cWin32 + cShellPrefix, False) then
Result:= r.ReadString (folder);
finally
r.Free;
end;
end;
It comes from Russell I think and the last piece from someone else whose name I forgot but me modified it a bit and so far so good. However I have been struggling quite some time now to achieve the following >
The button click cleans up 5 folders. Now I would want 5 checkboxes to determine whether all folders should be emptied. This way I can leave a folder out. I can't get that working. And I made it a little more difficult by using a third party component a TRzCheckgroup, hence that is where the checkboxes come from.
I have no idea how to get my hands on the individual items and tried surely everything I could come up with......so now I turn to you.
I'm willing to compromise and swap the third party checkgroup for 5 seperate boxes or whatever makes this easier. But so far I can't get it working regardless of what component I use
Hopefully some of you can.....the lbMain is a listbox since I wanted a bitmap to preceeed the feedback. Orignally it was a memo but I wanted something more appealing so I swapped the memo for the listbox and I'm quite happy wiht the result. It now displays bitmaps, either green or red.
I hope this is enough info otherwise please let me know......
Kindest regards,
Dweeep ;-)
I'm using this code to clean up some folders >
function TForm1.DeleteExclude(Path:
var listFiles: TStringList;
srFind: TSearchRec;
szPath: String;
dwSize: LongWord;
dwFiles: Integer;
dwFind: Integer;
begin
// Clear output
//Output.Clear;
// Make sure path is correct
szPath:=ExcludeTrailingBac
// Init the find
dwFind:=FindFirst(szPath+'
// Create list to hold files and paths
listFiles:=TStringList.Cre
// Resource protection
try
// Add progress
lbMain.AddObject(Format('[
lbMain.AddObject(Format('[
// Check find
if (dwFind = 0) then
begin
// Resource protection
try
// While sucess
while (dwFind = 0) do
begin
// Check name
if ((Length(srFind.Name) > 0) and not(srFind.Name[1] = '.')) then
begin
// Check path
if ((srFind.Attr and faDirectory) = faDirectory) then
// Recurse all files in folder
RecursePath(szPath+'\'+srF
// Compare against exclusion
else if (CompareText(srFind.Name, ExtractFileName(ExcludeFil
// Add to list with file size
listFiles.AddObject(szPath
end;
// Find next
dwFind:=FindNext(srFind);
end;
finally
// Close the find
FindClose(srFind);
end;
end;
// Set default result
result:=True;
// Check the list
if (listFiles.Count > 0) then
begin
// Set size and file count
dwFiles:=0;
dwSize:=0;
// Display file and directory count
for dwFind:=0 to Pred(listFiles.Count) do
begin
// Check for file
if (Integer(listFiles.Objects
begin
// Inc file count
Inc(dwFiles);
// Increment the total size
Inc(dwSize, Integer(listFiles.Objects[
end;
end;
// Add progress
lbMain.AddObject(Format('[
lbMain.AddObject(Format('[
lbMain.AddObject(Format('[
lbMain.AddObject(Format('[
// Allow updates
Application.ProcessMessage
// Reset size again
dwSize:=0;
// Now delete the file / paths
for dwFind:=Pred(listFiles.Cou
begin
// Check for path
if (Integer(listFiles.Objects
// Remove path
result:=RemoveDirectory(PC
else
begin
// Set file attr
SetFileAttributes(PChar(li
// Attempt to remove
result:=DeleteFile(PChar(l
// Check delete
if result then Inc(dwSize, Integer(listFiles.Objects[
end;
// Break on failure
if not(result) then break;
end;
// Check result
if not(result) then
lbMain.AddObject(Format('[
lbMain.AddObject(Format('[
lbMain.AddObject('',Image2
end
else
// Add progress
lbMain.AddObject(Format('[
lbMain.AddObject('',Image2
finally
// Free the list
listFiles.Free;
end;
end;
procedure TForm1.Button1Click(Sender
var dirStr: string;
begin
DeleteExclude(GetShellFold
DeleteExclude(GetShellFold
DeleteExclude(GetShellFold
DeleteExclude(GetShellFold
DeleteExclude(GetShellFold
DeleteExclude(GetFolder(di
end;
function TForm1.GetShellFolder (const folder: string):string;
const cWin32 = 'SOFTWARE\Microsoft\Window
cSHellPrefix = '\Explorer\Shell Folders';
var r: TRegistry;
begin
Result:= '';
r:= TRegistry.Create;
try
r.Rootkey:=HKEY_CURRENT_US
if r.OpenKey (cWin32 + cShellPrefix, False) then
Result:= r.ReadString (folder);
finally
r.Free;
end;
end;
It comes from Russell I think and the last piece from someone else whose name I forgot but me modified it a bit and so far so good. However I have been struggling quite some time now to achieve the following >
The button click cleans up 5 folders. Now I would want 5 checkboxes to determine whether all folders should be emptied. This way I can leave a folder out. I can't get that working. And I made it a little more difficult by using a third party component a TRzCheckgroup, hence that is where the checkboxes come from.
I have no idea how to get my hands on the individual items and tried surely everything I could come up with......so now I turn to you.
I'm willing to compromise and swap the third party checkgroup for 5 seperate boxes or whatever makes this easier. But so far I can't get it working regardless of what component I use
Hopefully some of you can.....the lbMain is a listbox since I wanted a bitmap to preceeed the feedback. Orignally it was a memo but I wanted something more appealing so I swapped the memo for the listbox and I'm quite happy wiht the result. It now displays bitmaps, either green or red.
I hope this is enough info otherwise please let me know......
Kindest regards,
Dweeep ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
KIndest regards,
Dweeeeeeeeeeep