Paulo Moura
Paulo Moura

Reputation: 18663

Inno Setup: renaming a directory fails

I'm trying to rename a directory using the RenameFile function without success. Using the DirExists function, I confirmed that the original directory exists and the the new directory doesn't exist. But still the rename fails. The directory paths only contain ASCII characters. I'm using Inno Setup 5.5.5. Tried both Unicode and non-Unicode versions. Same results. The code is:

procedure CurStepChanged(CurStep: TSetupStep);
var
  NewFolder, BackupFolder: String;
begin
  NewFolder := LgtUserDirPage.Values[0];
  Log('NewFolder: ' + NewFolder);
  if (CurStep = ssInstall) and DirExists(NewFolder) and (pos('backup', WizardSelectedComponents(False)) > 0) then begin
    BackupFolder := NewFolder + '-backup'
    Log('BackupFolder: ' + BackupFolder);
    if DirExists(NewFolder) then Log('Found!');
    if not DirExists(BackupFolder) then Log('No backup!');
    if RenameFile(NewFolder, BackupFolder) then Log('Backup created!');
  end
  else if (CurStep = ssPostInstall) then begin
    if FileExists(BackupFolder + '\settings.lgt') then
      FileCopy(BackupFolder + '\settings.lgt', NewFolder + '\settings.lgt', False);
    if FileExists(BackupFolder + '\settings.logtalk') then
      FileCopy(BackupFolder + '\settings.logtalk', NewFolder + '\settings.logtalk', False)
  end
end;

The debugging output is:

Setup application started
Setup version: Inno Setup version 5.5.5 (u)
Original Setup EXE: C:\Users\pmoura\Desktop\Output\logtalk-3.00.0-rc7.exe
Setup command line: /SL5="$3E0350,1739097,119296,C:\Users\pmoura\Desktop\Output\logtalk-3.00.0-rc7.exe" /SPAWNWND=$2D038A /NOTIFYWND=$220252 /DEBUGWND=$11027C 
Windows version: 6.1.7601 SP1  (NT platform: Yes)
64-bit Windows: Yes
Processor architecture: x64
User privileges: Administrative
64-bit install mode: No
Created temporary directory: C:\Users\pmoura\AppData\Local\Temp\is-JA0BI.tmp
NewFolder: C:\Users\pmoura\Documents\Logtalk
BackupFolder: C:\Users\pmoura\Documents\Logtalk-backup
Found!
No backup!

I never get the Backup created! output. The execution just go from that line to the end of the procedure. Anyone is aware of any reason that might explain why the renaming is failing? Disk space is not an issue, btw.

Upvotes: 1

Views: 3349

Answers (2)

Binary Thoughts
Binary Thoughts

Reputation: 33

Though its an old answer, I ran into the same issue, hopefully it can be helpful. Also used RenameFile for renaming the installation directory, and used a Repeat function to give the user the option to Retry in case the function fails.

Failure sometimes is logical: eg a program is running from the folder that is being renamed. But when the program is ended it still fails to rename the folder (after retry). Sometimes it than helps to also close Window File Explorer when having the folder opened (but not always!). In another case it just works perfectly, even when the folder is opened in Windows File Explorer.

In my case it could be due to the fact I'm renaming the installation directory, and am doing that on the PrepareToInstall event. Could be the installer is locking the directory in some cases?

My solution is to use SHFileOperation (link) for renaming directories (set wFunc -> FO_RENAME and pTo -> 'New Directory Name').

Upvotes: 1

Paulo Moura
Paulo Moura

Reputation: 18663

Found the problem. It turns out that the RenameFile was failing due to an open shell with a current directory inside the directory that I was trying to rename. In cases like these, it seems that the RenameFile simply returns false. A permission error (code) pointing out the problem would be more enlightening.

I have now updated my script to warn the user when the RenameFile fails, pointing to the likely cause. The update code can be browsed here.

Upvotes: 2

Related Questions