Search code examples
inno-setuppascalscript

Skip files during installation depending on text file command line argument


My scenario is currently that I'd like the possibility for users to skip installation of certain files that a user might have specified via a command line argument at install time.

The idea would be for the user to specify a text file and ideally during installation, the installer would check if the file currently being copied is listed in the supplied text file and decide depending on that.

My [Files] section atm is referencing full directories: would the best approach be to list all files individually and make use of the Check parameter or is there a different approach available? There seems to be no event function that would lend itself for this task.

Thanks Markus


Solution

  • No need to list all files individually. The Check function is called for each file individually, even when the Source is a wildcard.

    Use the CurrentFilename function to tell what file is being processed at the moment.

    Note that the Check function is called multiple times for each file.

    [Files]
    Source: "C:\path\*.*"; DestDir: "{app}"; Check: AllowFile
    
    [Code]
    
    var
      Whitelist: TStringList;
    
    function AllowFile: boolean;
    var
      FileName: string;
    begin
      FileName := ExtractFileName(ExpandConstant(CurrentFileName));
      Result := (Whitelist = nil) or (Whitelist.IndexOf(Uppercase(FileName)) >= 0);
      if Result then
        Log(Format('Allowing "%s"', [FileName]))
      else
        Log(Format('Skipping "%s"', [FileName]));
    end;
    
    function InitializeSetup(): Boolean;
    var
      WhitelistFile: string;
      Lines: TArrayOfString;
      I: Integer;
    begin
      WhitelistFile := ExpandConstant('{param:Whitelist}');
      if WhitelistFile = '' then
      begin
        Log('No whitelist specified, allowing all files');
      end
        else
      begin
        Whitelist := TStringList.Create;
        Whitelist.Sorted := True;
        LoadStringsFromFile(WhitelistFile, Lines);
        for I := 0 to GetArrayLength(Lines) - 1 do
          Whitelist.Add(Uppercase(Lines[I]));
        Log(Format('Loaded %d entries to whitelist from "%s"', [
          Whitelist.Count, WhitelistFile]));
      end;
      Result := True;
    end;