3
votes

How can I search for an existing exe file and then use that directory for my installer ?

If the exe file is not found I would like the user to browse for the path. In case the exe file is installed somewhere else.

Senario 1 (most common cases):

Default dir is c:\test\My program

This should be shown as the path on the "Select Destination Location" page When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program) If it exist, the user should just continue to the Ready to Install page.

Senario 2 (very seldom cases):

Default dir is c:\test\My program

This should be shown as the path on the "Select Destination Location" page When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program) If it does not exist, the user should be prompt for the path to "My program". The user should afterwards continue to the Ready to Install page. I then just trust that the user selects the correct path

How can I do this in InnoSetup ?

2
Do you mean you want to look for the path where your application was previously installed ? If so, then it's done "by default". Or do you want to have e.g. a custom page with a directory edit box prefilled by the searched directory ? - TLama
I mean: I have an Test application default installed to c:\program files\picture\picture.exe My Inno installer should then search for that path and copy 2-3 files to that dir. But if a customer has installed the test application somewhere else, I need Inno to search for picture.exe. If picture.exe is not found in the default directory the customer should just be told to browse for it. So Inno setup will copy the 2-3 files to the correct directory. Maybe there is a better way to do this. But there is no Registry string I can search for. I think your suggestion with a custom page sounds good - user302935
You should not search for a file but rather look in the registry or similar to find its real location, or check a few specific locations. Search a hard disk with many GB of data and millions of files will take forever. - Deanna
How was the "existing application already installed" installed in the first place? If it was installed via Inno-Setup and had a unique AppName/AppId you wouldn't have this problem updating your existing application - your update program would have had {app} variable automatically point to c:\program files\picture (or wherever it was initially installed to). - kobik
To answer the question asked you might want to look at: Scan a disk path or an entire disk with Inno Setup - Example script. Just a warning: Note that picture.exe could belong to another application, And you should have other means to 100% recognize it as yours (md5/version/etc). - kobik

2 Answers

4
votes

I do a similar thing with my installer already. The first thing I do is need to read the registry value from the program, and if that registry value is absent then I select that program's default directory. For example:

DefaultDirName={reg:HKLM\Software\Activision\Battlezone II,STInstallDir|reg:HKLM\Software\Activision\Battlezone II,Install121Dir|{pf32}\Battlezone II}

Now, the user runs the installer, and it has to check if the program is in the right folder. It does so by checking that the program's executable file already exists. I use this piece of code to do so.

{ Below code warns end user if he tries to install into a folder that does not contain bzone.exe. Useful if user tries to install into addon or any non-BZ2 folder. }
function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
  case CurPageID of
    wpSelectDir:
    if not FileExists(ExpandConstant('{app}\bzone.exe')) then begin
      MsgBox('Setup has detected that that this is not the main program folder of a Battlezone II install, and the created shortcuts to launch {#MyAppName} will not work.' #13#13 'You should probably go back and browse for a valid Battlezone II folder (and not any subfolders like addon).', mbError, MB_OK);
      end;
    wpReady:
  end;
  Result := True;
end;

The above code simply checks that the target executable exists and warns the user if it does not, giving him the chance to go back and change directories but also to go ahead with the install anyways.

Also, as you appear to be installing a patch or addon to an existing program, I recommend you set

DirExistsWarning=no
AppendDefaultDirName=false

And optionally to prevent an unnecessary screen if you are not creating start menu entries

DisableProgramGroupPage=yes
3
votes

I would make a file input page and let user choose the Picture.exe binary location manually, when it won't be found on expected location.

You can follow the commented version of this code:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output

[Files]
Source: "CurrentBinary.exe"; DestDir: "{app}"
Source: "PictureExtension.dll"; DestDir: "{code:GetDirPath}"

[Code]
var
  FilePage: TInputFileWizardPage;

function GetDirPath(const Value: string): string;
begin
  Result := '';
  if FileExists(FilePage.Values[0]) then
    Result := ExtractFilePath(FilePage.Values[0]);
end;         

procedure InitializeWizard;
var
  FilePath: string;
begin
  FilePage := CreateInputFilePage(wpSelectDir, 'Select Picture.exe location', 
    'Where is Picture.exe installed ?', 'Select where Picture.exe is located, ' +
    'then click Next.');
  FilePage.Add('Location of Picture.exe:', 'Picture project executable|Picture.exe', 
    '.exe');

  FilePage.Edits[0].ReadOnly := True;
  FilePage.Edits[0].Color := clBtnFace;

  FilePath := ExpandConstant('{pf}\Picture\Picture.exe');
  if FileExists(FilePath) then
    FilePage.Values[0] := FilePath;
end;