• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

How to do this in Delphi? (some DOS commands)

Howdie Folks,

I'm using a command script to get my hands on a directory which is created by windows each time a program is installed. I cannot predefine the name and so far the only way for me to get the directory is by using this command script: (I use it in my unattended installs btw)

FOR /f %%i IN ('DIR /b /a:d "%AppData%\Mozilla\Firefox\Profiles"') DO RMDIR /S /Q "%USERPROFILE%\Local Settings\Application Data\Mozilla\Firefox\Profiles\%%i\Cache"

Is there a Delphi way of doing this? I need to access the directory after it has been created but before the user interacts.

Kind regards,

Dweep

Ps working samples make me a happy man
0
PeterdeB
Asked:
PeterdeB
1 Solution
 
BedouinDNCommented:
This will need a cleanup and some error trapping but should do what you are after.
Put a button on a form and try it out :-)
//----------------------------------------------------------------------------------------------------
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    Function DelTree(DirName : string): Boolean;
    function GetShellFolder (const folder: string):string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
sRec: TSearchRec;
i: Integer;
begin
  i := sysutils.FindFirst(GetShellFolder('APPDATA') + '\Mozilla\Firefox\Profiles\*.*',faAnyFile,sRec);
    while i = 0 do
    begin
      if (sRec.Name <> '.') and (sRec.Name <> '..') then
      begin
        if ((sRec.Attr and faDirectory) = faDirectory) then
        begin
          DelTree(GetShellFolder('Local Appdata') + '\Mozilla\Firefox\Profiles\' + sRec.Name + '\Cache');
        end;
      end;
    i := SysUtils.FindNext(sRec);
    end;
  SysUtils.FindClose(sRec);
end;

Function TForm1.DelTree(DirName : string): Boolean;
var
  SHFileOpStruct : TSHFileOpStruct;
  DirBuf : array [0..255] of char;
begin
  try
   Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
   FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
   StrPCopy(DirBuf, DirName) ;
   with SHFileOpStruct do begin
    Wnd := 0;
    pFrom := @DirBuf;
    wFunc := FO_DELETE;
    fFlags := FOF_ALLOWUNDO;
    fFlags := fFlags or FOF_NOCONFIRMATION;
    fFlags := fFlags or FOF_SILENT;
   end;
    Result := (SHFileOperation(SHFileOpStruct) = 0) ;
   except
    Result := False;
  end;
end;

function TForm1.GetShellFolder (const folder: string):string;
const cWin32 = 'SOFTWARE\Microsoft\Windows\CurrentVersion';
      cSHellPrefix = '\Explorer\Shell Folders';
var r: TRegistry;
begin
  Result:= '';
  r:= TRegistry.Create;
    try
      r.Rootkey:=HKEY_CURRENT_USER;
        if r.OpenKey (cWin32 + cShellPrefix, False) then
      Result:= r.ReadString (folder);
    finally
    r.Free;
    end;
end;
//---------------------------------------------------------------------------------------------------
Cheers.
Colin.
0
 
PeterdeBAuthor Commented:
I'm still trying to get it to really delete all the junk, but you answered my question so here ya go mate!!

Kind regards,

Dweep ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now