Inno Setup Conditional Copying

August 3, 2011

I have a project which makes use of Inno Setup, and I have a requirement to copy some files if they are present in the same directory as the installer. However, I didn't want to copy them unconditionally, as this could lead to data loss in some circumstances.

I decided to only do the copying if a given environment variable was set to the product version number. This was done by adding a small pascal script in the installer, and then using the Check option on the file copying line.

Here's the Code block used. $$VERSION$$ is replaced by the release version at build time.

[Code]
function CopyInitialRepos : Boolean;
begin
    if GetEnv('DASHBOARD_COPY_REPOSITORY') = '$$$VERSION$$$' then begin
      Result := True;
    end else begin
      Result := False;
   end;
end;

Then, in the Files section, we add the Check clause to run this function:

[Files]
Source: "{src}\db.script"; DestDir: "{app}\data"; Flags: external ignoreversion skipifsourcedoesntexist; Check: CopyInitialRepos

Source: "{src}\db.log"; DestDir: "{app}\data"; Flags: external ignoreversion skipifsourcedoesntexist; Check: CopyInitialRepos

Tags: inno setup pascal env