Search code examples
installationdirectorynsis

NSIS - Install a file to a pre-existing, uncertain subfolder?


I'm trying to install a file to a pre-existing folder structured like this:

$APPDATA/somefolder/(uncertainFolder)

The "uncertainFolder" would be either "1.0" or "2.0".

The same file will be installed into the "uncertainFolder" despite the folder name difference. How can I achieve this?

Thank you in advance.


Solution

  • Files installed with the File instruction are extracted into the directory set by SetOutPath. Changing this path at run-time is not a problem once you know which folder you want.

    If the possible folder names are known at compile-time you can use if/or if/else:

    !include LogicLib.nsh
    ${If} ${FileExists} "$InstDir\SomeFolder\2.0\*.*"
        SetOutPath "$InstDir\SomeFolder\2.0"
    ${Else}
        SetOutPath "$InstDir\SomeFolder\1.0"
    ${EndIf}
    

    You can also enumerate files and folders at run-time:

    FindFirst $0 $1 "$InstDir\SomeFolder\*.*"
    loop:
        StrCmp $1 "" end ; No more files?
        StrCmp $1 "." next ; DOS special name
        StrCmp $1 ".." next ; DOS special name
        IfFileExists "$InstDir\SomeFolder\$1\*.*" 0 next ; Skip files
        DetailPrint "$1 is a folder in $InstDir\SomeFolder"
        SetOutPath "$InstDir\SomeFolder\$1"
        Goto end ; This stops the search at the first folder it finds
    next:
        FindNext $0 $1
        goto loop
    end:
    FindClose $0
    

    The Locate macro in FileFunc.nsh is built on top of FindFirst/FindNext and can also be used if you prefer its syntax...