inno-setupinno-setup-v6

How to unpack files conditionally, depend on the content of target folder in Inno Setup?


I have to prepare an installer for my app. The app depends on two config files, which are located in the config subfolder of an app. I`ve added the line to my InnoSetup Copiler script:

Source: "C:\Path\to\my\source\app\files\config\*"; DestDir: "{app}\config"; Flags: ignoreversion recursesubdirs createallsubdirs

and it works well, but when I am using script to installation brand new app on the machine. What I would like to achive is to installer not to override selected config files in config location, when it discovers their existence. The idea is to use the same Installer to install brand new app and update the existing one, but without overrideing user config files.

I`ve tried to check if files are in location, but to be honest Delphi/Pascal is not my strongest skill, and my efforts did not bring any working result.

Thanks in advance.


Solution

  • To conditionally unpack files in Inno Setup based on the existence of target files, you can use the Check parameter in your script and write a custom function to determine if the file exists in the target directory. Here's how you can achieve this:

    CODE:
    [Files]
    Source: "C:\Path\to\my\source\app\files\config\file1.cfg"; DestDir: "{app}\config"; Flags: ignoreversion; Check: ShouldCopyFile('file1.cfg')
    Source: "C:\Path\to\my\source\app\files\config\file2.cfg"; DestDir: "{app}\config"; Flags: ignoreversion; Check: ShouldCopyFile('file2.cfg')
    
    [Code]
    function ShouldCopyFile(FileName: string): Boolean;
    var
      TargetFilePath: string;
    begin
      TargetFilePath := ExpandConstant('{app}\config\') + FileName;
    
      // Check if the file exists in the target directory
      if FileExists(TargetFilePath) then
      begin
        // File exists, do not copy it
        Result := False;
      end
      else
      begin
        // File does not exist, proceed with copying
        Result := True;
      end;
    end;
    

    Define Each File Explicitly: The Source lines specify individual files from the config folder. You need to list all the files explicitly in this approach.

    Check Parameter: The Check parameter is used to conditionally determine whether the file should be copied.

    Custom Function ShouldCopyFile:

    It checks if the target file exists using FileExists. If the file exists, the function returns False, preventing the file from being copied. If the file does not exist, it returns True, allowing the file to be copied. Expand Constants: The `ExpandConstant function resolves {app} to the actual installation directory.