With Inno Setup how can I rename and copy a file a user chooses (an image) to the program files directory

file-io, inno-setup

Solution

How to create a filtered input file wizard page item ?

For the purpose of file selections you should use a separate input file wizard page, which you can create by the `CreateInputFilePage`. Then, the `Add` method of the `TInputFileWizardPage` page object class contains the `AFilter` parameter where you can specify the filter for files that can be selected by its associated file open dialog. In this example we allow the users select only *.jpg files:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
var
  InputPage: TInputFileWizardPage;

procedure InitializeWizard;
begin
  // create the input file wizard page
  InputPage := CreateInputFilePage(wpWelcome, 'Caption', 'Description',
    'SubCaption');
  // and insert one item in which the user will be restricted to select
  // only *.jpg files
  InputPage.Add('Prompt', 'JPG files (*.jpg)|*.jpg', '.jpg');
end;

How to manually invoke a filtered file open dialog ?

If that input file wizard page doesn't fit to your design and you want to make your own, then you need the `GetOpenFileName` function to show an open file dialog. Even this function contains a parameter where you can specify the filter string. In this case it is the `Filter` parameter. In this script example is shown, how to invoke a file open dialog with a *.jpg file filter:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
procedure InitializeWizard;
var
  FileName: string;
begin
  if GetOpenFileName('Prompt', FileName, 'C:\InitialDirectory',
    'JPG files (*.jpg)|*.jpg', '.jpg')
  then
    MsgBox(Format('Selected file: %s', [FileName]), mbInformation, MB_OK);
end;

How to build a filter string ?

As you can see in the above code, the filter string consists from the caption part and the filtering portion separated by the `|` char. You can also specify multiple filters by separating each filter string, again, by the `|` char, or you can specify multiple extensions for a single filter by separating extensions by `;`.

So, e.g. to create a filter named `JPG files` filtering only *.jpg files you can write:

JPG files|*.jpg

To make two filters, let's say one for *.jpg files and one for *.jpeg files you can write:

JPG files|*.jpg|JPEG files|*.jpeg

And finally, to make a single filter for *.jpg and *.jpeg files you can write:

JPEG files|*.jpg;*.jpeg

For details I would refer you to the Delphi reference page for the `TOpenDialog.Filter` property.

Problem

I understand how to create a custom wizard page. Just not sure how to add a file picker, and restrict it to a .jpg file type. I have a code section that runs at the end of the installation to take user defined input during setup and modify a settings file, just need to understand how to take the value of a file they choose during a setup screen step, and then copy it, rename it, and dump it into the program files folder.

Original source