Search code examples
nsis

In NSIS, how can I rename or copy a file with a user variable?


I manage to create a basic installer, but I dislike the version number in the exe name.

I would like to rename the file to a more generic one such as myapp.exe instead of "myapp_v1.2.3.exe"

I read the documentation, but it's not clear about the usage of user variable. I always get an error telling $var name does not exists.

Section "Godot" SecGodot

  SetOutPath "$INSTDIR"
  
  ;ADD YOUR OWN FILES HERE...
  StrCpy $originalName "Godot_v3.3.4-stable_mono_win64.exe"
  StrCpy $exeName "Godot.exe"

  File "${originalName}" #<----------- ERROR HERE : File: "${originalName}" -> no files found.
  Rename "${originalName}" "{$exeName}"

  SetOutPath "$INSTDIR\GodotSharp"
  File /nonfatal /a /r "GodotSharp\"
  
  ;Store installation folder
  WriteRegStr HKCU "Software\Godot" "" $INSTDIR
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    
    ;Create shortcuts
    CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
    CreateShortcut "$SMPROGRAMS\$StartMenuFolder\Uninstall.lnk" "$INSTDIR\Uninstall.exe"

    CreateShortcut "$SMPROGRAMS\$StartMenuFolder\Godot.lnk" "$INSTDIR\${exeName}"
  
  !insertmacro MUI_STARTMENU_WRITE_END

SectionEnd

Thanks for helping


Solution

  • Variables can only be expanded when running on the end-users machine. File needs the name of the file on your machine. To do that, use a define:

    !define foo "bar"
    File "${foo}.exe" ; bar.exe
    

    This does not help you change the name, it only shows how defines are used.

    In this specific case you don't need a define, you can set a specific output name:

    SetOutPath $InstDir
    File "/oname=Godot.exe" "Godot_v3.3.4-stable_mono_win64.exe"