1
votes

I would like to dynamically name my EXE files based on the folder in which they're being built. So, in other words, if my NSIS script is located in C:\[blah]\MyProjectDir\Nullsoft\ and my project is located in C:\[blah]\MyProjectDir\MyProject\ the output NSIS executable should be MyProjectDir.exe. I'm obviously obfuscating the names for simplicity, the structure of the folders has actual meaning to our team.

Basically the our (already written, ages ago) NSIS scripts look something like this:

!define APP_NAME "OurGUI"
!define PROJ_NAME "OurGUI"
!define PATH_TO_FILES ..\${PROJ_NAME}\bin\Debug
!define EXEC_NAME "OurGUI.exe"
OutFile "GUI_Setup.exe"

There's a whole bunch more that handle things like registry keys, installation, etc... this is the important part as it pertains to my question (because I don't want to modify the rest of the script).

I'd like to change it to something like this

!define M_NUMBER !system "for %I in (..) do echo %~nxI"

!define APP_NAME "${M_NUMBER}/ Demo GUI"
!define PROJ_NAME "OurGUI"
!define PATH_TO_FILES ..\${PROJ_NAME}\bin\Debug
!define EXEC_NAME "M_Number.exe"
OutFile "${M_NUMBER}_GUI_Setup.exe"

I know my syntax is totally off, but that's the idea.

Running the command for %I in (..) do echo %~nxI returns to me the name of the grandparent directory, which is exactly what I want. Now I need to figure out how to store it in some sort of variable so that it can be used in the !define's. I need to somehow capture the output to the console (not the return value of for, which is always zero, within my NSIS script. Ideally I wouldn't have to use external plugins because that'll be a nightmare to manage across the team.

As a nice bonus, I'd also love to be able to manipulate the string (again, at compile time so it can be used in the !define's.

The strings follow the format: SWD1234-567, but I would like to store it as D1234567 in another variable for other renaming purposes. So, drop the "SW" and the "-" from the original string.

I'm open to other suggestions, but as a newbie to NSIS, this is what I've come up with.

1
Can you not set an environmental variable with a batch file before you run the NSIS script and then read that environmental variable like this: $%envVarName% - Squashman
I'd rather not run a batch file before because I don't want to change the process of doing a release. But no reason I can't run it at the beginning of the script or something with !system or execwait - audiFanatic
So are you saying you can do this: !system "for %I in (..) do set envVarName=%~nxI" - Squashman
Oh idk. I doubt that's legal, doesn't compile for me. Just trying to convey the idea - audiFanatic
You would probably have to use cmd.exe !system 'cmd /c "for %I in (..) do set envVarName=%~nxI'" - Squashman

1 Answers

3
votes

You need to use the !system+!include idiom to read the output of external commands:

!tempfile MYINCFILE
!system 'for %I in (..) do echo !define MYDIR "%~nxI" > "${MYINCFILE}"'
!include "${MYINCFILE}"
!delfile "${MYINCFILE}"
!echo "${MYDIR}"

The pre-processor has limited string handling but removing known sub-strings is possible:

!define MYDIR "SWD1234-567"
!searchreplace MYDIR2 "${MYDIR}" "SW" ""
!searchreplace MYDIR2 "${MYDIR2}" "-" ""
!echo "${MYDIR2}" ; D1234567