Search code examples
installationinno-setuppascalscript

Inno Setup RenameFile and FileCopy inexplicably failing on some computers


My app has several gigabytes of image data which used to be stored in the {app} directory. In the next update I need to move that picture data, as part of the Inno Setup installation, to the {commonappdata} directory in order to be a well behaved program.

I created a procedure that attempts to move a directory full of images (identified by the InName parameter) by renaming it (for speed purposes). If that fails (i.e. if the directories were on different drives) then the procedure will copy the file and then delete the source.

All that works great on my computer and any computers I have access to, but for most other people it fails to rename the directory and then fails to copy it. The procedure logs all of this so I know that the source and destination names are correct, but the RenameFile and FileCopy just fail without any explanation.

I'd really appreciate any thoughts anyone has as to why the RenameFile and FileCopy commands are failing on other people's computers but not mine. I've verified that they can copy the files using File Explorer without any issues (they're not read-only or anything like that).

Procedure MoveIt(InName: string);
var Sstr,Dstr: string;
begin
  Sstr:=ExpandConstant('{app}')+'\images\'+InName;
  Dstr:=ExpandConstant('{commonappdata}')+'\MyApp\images\'+InName;
  Log('Source: '+Sstr);
  Log('Destination: '+Dstr);
  if DirExists(Sstr) then
  begin
    OutputMsgMemoWizardPage.RichEditViewer.lines.add(InName+'...');
    if RenameFile(ExpandConstant('{app}')+'\images\'+InName,ExpandConstant('{commonappdata}')+'\MyApp\images\'+InName) then
    begin
      Log('Rename result=success');
      OutputMsgMemoWizardPage.RichEditViewer.lines[OutputMsgMemoWizardPage.RichEditViewer.lines.Count-1]:=InName+': success';
    end else
    begin
      Log('Rename result=Failed'); 
      if FileCopy(ExpandConstant('{app}')+'\images\'+InName,ExpandConstant('{commonappdata}')+'\MyApp\Folder images\'+InName,true) then
      begin
        Log('Copy result=success');
        OutputMsgMemoWizardPage.RichEditViewer.lines[OutputMsgMemoWizardPage.RichEditViewer.lines.Count-1]:=InName+': success';
        if DeleteFile(ExpandConstant('{app}')+'\images\'+InName) then
          Log('Delete result=success')
        else
        begin
          Log('Delete result=Failed');
          OutputMsgMemoWizardPage.RichEditViewer.lines.add(InName+
            '(Could not delete source directory)');
        end;
      end else
      begin
        Log('Copy result=Failed');
     OutputMsgMemoWizardPage.RichEditViewer.lines[OutputMsgMemoWizardPage.RichEditViewer.lines.Count-1]:=InName+': Failed';
      end;
    end
  end else Log('Source file not found');
end;

Solution

  • Make sure the target directories do exist. I.e. those like {commonappdata}\MyApp\images.

    The RenameFile nor FileCopy functions won't create them for you.


    You can also check the GetLastError after the function fails. For a declaration, see Inno Setup FileExists unable to find existing file.