how to locate the 'Temporary Internet Files' directory?

how to find 'Temporary Internet Files' directory in program and delete some files?
strangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
hi,
this is for nt:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
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_LOCAL_MACHINE;
If TheReg.OpenKey('Software\Microsoft\Internet Explorer\Main\', False ) then
begin
TempString := TheReg.ReadString('Cache_Directory');
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] );
combobox1.items.add(TheList[i]);
except
end;
End;
finally
TheList.Free;
end;
showmessage('finished');
end;

end.



for win9* pc's just change the registry path to hkcu \Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\ and read the string "cache" .


here another example maybe works on both (untested on nt):

unit UrlH_Unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Activex, Comobj, StdCtrls, ComCtrls, Grids;

const

  STATURL_QUERYFLAG_ISCACHED = $00010000;
  STATURL_QUERYFLAG_NOURL = $00020000;
  STATURL_QUERYFLAG_NOTITLE = $00040000;
  STATURL_QUERYFLAG_TOPLEVEL = $00080000;

  STATURLFLAG_ISCACHED = $00000001;
  STATURLFLAG_ISTOPLEVEL = $00000002;


  IID_IEnumStatUrl: TGUID = (D1: $3C374A42; D2: $BAE4; D3: $11CF; D4: ($BF, $7D, $00, $AA, $00, $69, $46, $EE));
  IID_IUrlHistoryStg: TGUID = (D1: $3C374A41; D2: $BAE4; D3: $11CF; D4: ($BF, $7D, $00, $AA, $00, $69, $46, $EE));
  IID_IUrlHistoryStg2: TGUID = (D1: $AFA0DC11; D2: $C313; D3: $831A; D4: ($83, $1A, $00, $C0, $4F, $D5, $AE, $38));
  IID_IUrlHistoryNotify: TGUID = (D1: $BC40BEC1; D2: $C493; D3: $11D0; D4: ($83, $1B, $00, $C0, $4F, $D5, $AE, $38));

  SID_IEnumStatUrl = '{3C374A42-BAE4-11CF-BF7D-00AA006946EE}';
  SID_IUrlHistoryStg = '{3C374A41-BAE4-11CF-BF7D-00AA006946EE}';
  SID_IUrlHistoryStg2 = '{AFA0DC11-C313-11d0-831A-00C04FD5AE38}';
  SID_IURLHistoryNotify = '{BC40BEC1-C493-11d0-831B-00C04FD5AE38}';
  CLSID_CUrlHistory: TGUID = '{3C374A40-BAE4-11CF-BF7D-00AA006946EE}';


type
  STATURL = record
    cbSize: DWORD;
    pwcsUrl: DWORD;
    pwcsTitle: DWORD;
    ftLastVisited: FILETIME;
    ftLastUpdated: FILETIME;
    ftExpires: FILETIME;
    dwFlags: DWORD;
  end;


  IEnumSTATURL = interface(IUnknown)
    ['{3C374A42-BAE4-11CF-BF7D-00AA006946EE}']
    function Next(celt: Integer; out elt; pceltFetched: PLongint): HRESULT; stdcall;
    function Skip(celt: Longint): HRESULT; stdcall;
    function Reset: HResult; stdcall;
    function Clone(out ppenum: IEnumSTATURL): HResult; stdcall;
    function SetFilter(poszFilter: PWideChar; dwFlags: DWORD): HResult; stdcall;
  end;

  IUrlHistoryStg = interface(IUnknown)
    ['{3C374A41-BAE4-11CF-BF7D-00AA006946EE}']
    function AddUrl(pocsUrl: PWideChar; pocsTitle: PWideChar; dwFlags: Integer): HResult; stdcall;
    function DeleteUrl(pocsUrl: PWideChar; dwFlags: Integer): HResult; stdcall;
    function QueryUrl(pocsUrl: PWideChar; dwFlags: Integer; var lpSTATURL: STATURL): HResult; stdcall;
    function BindToObject(pocsUrl: PWideChar; var riid: TIID; out ppvOut: Pointer): HResult; stdcall;
    function EnumUrls(out ppenum: IEnumSTATURL): HResult; stdcall;
  end;

  IUrlHistoryStg2 = interface(IUrlHistoryStg)
    ['{AFA0DC11-C313-11D0-831A-00C04FD5AE38}']
    function AddUrlAndNotify(pocsUrl: PWideChar; pocsTitle: PWideChar; dwFlags: Integer;
      fWriteHistory: Integer; var poctNotify: Pointer;
      const punkISFolder: IUnknown): HResult; stdcall;
    function ClearHistory: HResult; stdcall;
  end;

  IUrlHistoryNotify = interface(IOleCommandTarget)
    ['{BC40BEC1-C493-11d0-831B-00C04FD5AE38}']
  end;



  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    RetrieveBtn: TButton;
    ClearBtn: TButton;
    procedure RetrieveBtnClick(Sender: TObject);
    procedure ClearBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  I: IUrlHistoryStg2;

implementation

{$R *.DFM}


function FileTimeToDt(Ft: TFileTime): TDateTime;
var
  l: Integer;
  lft: TFileTime;
begin
  FileTimeToLocalFiletime(Ft, lft);
  if FileTimeToDosDateTime(lft, Longrec(l).Hi, Longrec(l).Lo) then
    result := FiledateToDatetime(l) else
    result := 0;
end;


procedure TForm1.RetrieveBtnClick(Sender: TObject);
var
  r: Staturl;
  Enum: IEnumStatUrl;
  X: Integer;
  Row: Integer;
begin
  StringGrid1.Cells[0, 0] := 'Last Visited';
  StringGrid1.Cells[1, 0] := 'Title';
  StringGrid1.Cells[2, 0] := 'Url';
  StringGrid1.Cells[3, 0] := 'Last Updated';
  StringGrid1.Cells[4, 0] := 'Expires';
  Row := 0;
  I := CreateComObject(ClsId_CUrlHistory) as IUrlHistoryStg2;
  I.EnumUrls(Enum);
  Enum.SetFilter('http://', 0);
  while enum.next(1, r, @X) = S_OK do begin
    Inc(Row);
    StringGrid1.RowCount := Row + 1;
    Stringgrid1.Cells[0, Row] := DateTimeToStr(FileTimeToDt(r.ftLastVisited));
    Stringgrid1.Cells[1, Row] := PWidechar(Pointer(r.pwcsTitle));
    Stringgrid1.Cells[2, Row] := PWidechar(Pointer(r.pwcsUrl));
    Stringgrid1.Cells[3, Row] := DateTimeToStr(FileTimeToDt(r.ftLastUpdated));
    Stringgrid1.Cells[4, Row] := DateTimeToStr(FileTimeToDt(r.ftExpires));
  end;
end;

procedure TForm1.ClearBtnClick(Sender: TObject);
begin
I.ClearHistory;
end;

end.





form as text:



object Form1: TForm1
  Left = 192
  Top = 107
  Width = 763
  Height = 349
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 0
    Top = 0
    Width = 755
    Height = 257
    Align = alTop
    DefaultRowHeight = 14
    FixedCols = 0
    TabOrder = 0
    ColWidths = (
      103
      205
      207
      113
      150)
  end
  object RetrieveBtn: TButton
    Left = 208
    Top = 282
    Width = 137
    Height = 25
    Caption = 'Enumerate UrlHistory'
    TabOrder = 1
    OnClick = RetrieveBtnClick
  end
  object ClearBtn: TButton
    Left = 448
    Top = 282
    Width = 137
    Height = 25
    Caption = 'Clear UrlHistory'
    TabOrder = 2
    OnClick = ClearBtnClick
  end
end

Regards Barry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
note:
before deleting anything make sure the examples are returning the correct paths as you dont wanna delete your c:\ drive by mistake ;-)
0
strangAuthor Commented:
I try the code
found the registry path in my computer is 'Software\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\Cache\content\' and readstring is 'CachePath'
I use IE5 in my computer, but how can I know the registry path in other computers?
and ,I want to delete cookies files in temporary directory,but I can't find them with your program.
0
intheCommented:
was there not a path here:

Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\ and read the string "cache" ?
there should be..

 ok here is another method for win9* machines:

either use
const
 CSIDL_INTERNET_CACHE   = $0020;
 CSIDL_COOKIES      = $0021;

or add Shlobj to the uses section
then:

 Var
  SFolder :  pItemIDList;
  SpecialPath : Array[0..MAX_PATH] Of Char;
begin
SHGetSpecialFolderLocation(Form1.Handle, CSIDL_INTERNET_CACHE, SFolder);
SHGetPathFromIDList(SFolder, SpecialPath);
Label1.Caption := StrPas(SpecialPath);


but you  must not use that on nt without checking the return path first becuase i have problems in past with it.

did you try the second method ?
what operating system are you testing on?

also see:
http://www.euromind.com/iedelphi/ 
under urlHistory for 3 examples of getting history.
one of them was 2 method i posted here.


0
rwilson032697Commented:
Listening
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.