Search code examples
inno-setuppascalscript

Inno Setup Runtime error while accessing CurrentFileName when installing an empty folder


I tried to access all files inside a folder through Pascal code. When I iterate through an empty folder I am getting the following error:

Exception: Internal error; An attempt was made to call the "CurrentFileName" function from outside a "Check", "BeforeInstall" or "AfterInstall" event function belonging to a "[Files]" entry

The code I used:

[Files]
Source: ".\3D_Dlls\*"; DestDir: {app}\3D_Dlls; \
    Flags: ignoreversion recursesubdirs createallsubdirs skipifsourcedoesntexist; \
    Check: FileBackup    

[Code]
function FileBackup(): Boolean; 
var FileName,Source,Target,TargetDir: String; 
begin 
  Result := True;
  Source := ExpandConstant(CurrentFileName);
end

Say for example my folder structure is like that:

3D_Dlls
-   Folder 1
-   Folder 2 // Empty folder and it invokes the problem
-   Folder 3

Solution

  • I believe this is a bug in Inno Setup triggered by the createallsubdirs flag.

    When the flag is specified, Inno Setup collects a separate list of empty directories that need to be explicitly created during an installation. When "installing" the empty folder, the call to the CurrentFileName simply fails.

    Workarounds:

    • Remove the createallsubdirs flag, if possible.

      Instead, you can explicitly create empty directories using the [Dirs] section.

    • Catch the exception:

      function FileBackup: Boolean; 
      begin 
        Result := True;
        try
          Source := ExpandConstant(CurrentFileName);
        except
          Log(GetExceptionMessage);
        end;
      end;
      

    I have reported this on Inno Setup newsgroup, but the newsgroup was reset since.