Search code examples
cmdinno-setuppascalscript

How can I rename a file in code in Inno Setup?


I need to rename a file to zip, I try this code but doesn't works.

 Exec('cmd.exe', 'rename '+ExpandConstant('{app}\scripts\set.bat'+' set.zip'), '', SW_SHOW,
      ewWaitUntilTerminated, ResultCode);

I found a way to do it on [Run] section, but I need it to do in [Code] section.


Solution

    1. You need to quote the paths, as {app} typically contains spaces (Program Files).

    2. You need /C command-line parameter before the command.

    Exec('cmd.exe',
         '/C rename ' + AddQuotes(ExpandConstant('{app}\scripts\set.bat')) + ' set.zip',
         '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
    

    Though as @Alex correctly commented, use RenameFile instead:

    RenameFile(ExpandConstant('{app}\scripts\set.bat'), ExpandConstant('{app}\scripts\set.zip'));