Link to home
Start Free TrialLog in
Avatar of reddarin
reddarin

asked on

For inthe - 10205304

I had found a question on deleting temp internet files and promised to award points for the question separately from the other person that had posted the question. I hadn't realized that so much time had gone by, my apologies to inthe.

The answer from inthe ...

like so:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Registry;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function FindTemporaryFiles : String;
var
  TheReg : TRegistry;
  TempString : String;
begin
  TheReg := TRegistry.Create;
  try
    TheReg.RootKey := HKEY_CURRENT_USER;
    If TheReg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\', False ) then
      begin
        TempString := TheReg.ReadString('Cache');
        If Copy(TempString, Length(TempString) - 1, 1) <> '\' then
          TempString := TempString + '\';
        Result := TempString;
      end
    else
      Result := 'NOTHING';
  finally
    TheReg.Free;
  end;
end;

procedure GatherFiles(Const Lines: TStrings; Const Dir,Match: String);
var
  Rec: TSearchRec;
  Found: Integer;
Begin
  // Add files
  Found:= FindFirst(Dir+'\'+Match, faAnyFile, Rec);
  While Found = 0 do
    begin
      If (Rec.Attr AND faDirectory) = 0 Then Lines.Add(Dir+'\'+Rec.Name);
        Found := FindNext(Rec);
    End;
  FindClose(Rec);
  // Find directories (Without match)
  Found:= FindFirst(Dir+'\*.*', faAnyFile, Rec);
  While Found = 0 do
    begin
      If ((Rec.Attr AND faDirectory)>0) AND (Rec.Name<>'.') AND (Rec.Name<>'..') Then
        GatherFiles(Lines,Dir+'\'+Rec.Name,Match);
      Found := FindNext(Rec);
    End;
  FindClose(Rec);
End;

procedure TForm1.Button1Click(Sender: TObject);
Var
  TempPath : String;
  TheList : TStringList;
  i : integer;
begin
  TheList := TStringList.Create;
  try
    TempPath := FindTemporaryFiles;
    If (length(TempPath) > 3) AND (UPPERCASE(TempPath) <> 'C:\WINDOWS') AND (UPPERCASE(TempPath) <> 'C:\WINNT') then
      begin
        GatherFiles(TheList,TempPath,'*.*');
        For i := 0 to TheList.Count - 1 do
            try
              DeleteFile( TheList[i] );
            except
           end;
      End;
    finally
      TheList.Free;
    end;
    showmessage('finished');
 end;


end.

Regards Barry
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of reddarin
reddarin

ASKER

Adjusted points to 200
Thanks again Barry =)