20

I need my installer to check if a file exists in the destination location, and if is not there, then the installation aborts. My project is a update patch, so I want the installer to avoid installing the update files if the main exe of the application is not in the destination. How can I do this?

Can someone give an example of code to check file version through the Windows registry?

[Files]
Source C:\filename.exe; DestDir {app}; Flags: ignoreversion; BeforeInstall: CheckForFile;

[code]

procedure CheckForFile(): Boolean;
begin
  if (FileExists('c:\somefile.exe')) then
  begin
    MsgBox('File exists, install continues', mbInformation, MB_OK);
    Result := True;
  end
  else
  begin
    MsgBox('File does not exist, install stops', mbCriticalError, MB_OK);
    Result := False;
  end;
end;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dielo
  • 603
  • 2
  • 12
  • 25
  • 1
    Normally, for update installers, you just use the same AppID, and Inno will handle the rest for you. See [this article](http://www.vincenzo.net/isxkb/index.php?title=Upgrades) for more details. – Deanna Oct 19 '12 at 15:51

2 Answers2

21

Just don't let the user proceed until they pick the correct folder.

function NextButtonClick(PageId: Integer): Boolean;
begin
    Result := True;
    if (PageId = wpSelectDir) and not FileExists(ExpandConstant('{app}\yourapp.exe')) then begin
        MsgBox('YourApp does not seem to be installed in that folder.  Please select the correct folder.', mbError, MB_OK);
        Result := False;
        exit;
    end;
end;

Of course, it's also a good idea to try to automatically pick the correct folder for them, eg. by retrieving the correct location out of the registry.

Miral
  • 12,637
  • 4
  • 53
  • 93
  • So then you should [`accept the answer`](http://meta.stackexchange.com/a/5235/179541). Anyway, you've said you want to abort the installation in your question. Well, never mind... – TLama Oct 20 '12 at 07:54
  • This is effectively an abort. For an interactive installation it will not give the user any options other than to correct the path or to cancel. For a non-interactive (silent) installation it will just abort. (Though on that note, if you're expecting silent installs you should use `SuppressibleMsgBox` instead.) – Miral Oct 21 '12 at 07:04
  • Yeah this works perfect...the other answer you give me bro works too.. its just that this one its less complicated for noobs like me :( – Dielo Oct 21 '12 at 15:56
6

Another solution would be the InitializeSetup():

Credit: Manfred

[code]
   function InitializeSetup(): Boolean;
   begin
     if (FileExists(ExpandConstant('{pf}\{#MyAppName}\somefile.exe'))) then
     begin
       MsgBox('Installation validated', mbInformation, MB_OK);
       Result := True;
     end
     else
     begin
       MsgBox('Abort installation', mbCriticalError, MB_OK);
       Result := False;
     end;
   end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Advena
  • 1,664
  • 2
  • 24
  • 45
  • thank you. This code is very useful. but I put another situation: if not abort, but continue (?!) to be just information. – Sweeper Jul 31 '19 at 11:01