Skip to content
⌘ NSIS Forum Archive

Manipulating the string produced by EXEDIR

6 posts

fitzroy_doll#edited

Manipulating the string produced by EXEDIR

I am sure this is a well documented feature, but I'm not sure what I'm looking for.

I would like to set an output directory using the EXEDIR path, but am not sure how to manipulate path strings to move up directories.

I would like to achieve the following:

1. Installer detects path to itself, then saves part of the path to $R0, and another part to $R1

Eg

Installer is in C:\Program Files\my app\my folder\target folder\installer.exe

Thus

EXEDIR = C:\Program Files\my app\my folder\target folder\

$R0 = C:\Program Files\my app
$R1 = C:\Program Files\my app\my folder

How is this done?

Thank you.

Edit: Something like this, but with one function for each output above?

from http://nsis.sourceforge.net/Get_first_part_of_a_string

Push $EXEDIR ; Input string
Call GetFirstStrPart
Pop "$R0"

Where $R0 will now be "C:\Program Files\my app"

The Function

Function GetFirstStrPart
Exch $R0
Push $R1
Push $R2
StrLen $R1 $R0
IntOp $R1 $R1 + 1
loop:
IntOp $R1 $R1 - 1
StrCpy $R2 $R0 1 -$R1
StrCmp $R2 "" exit2
StrCmp $R2 "my folder\target folder\" exit1 ;the directories to cut off
Goto loop
exit1:
StrCpy $R0 $R0 -$R1
exit2:
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
demiller9#
E.1.15 GetParent does what you are asking for.

The section intro even shows a short example:
Include header:

!include "FileFunc.nsh"

Call functions:

Section Install
${GetFileExt} "C:\My Downloads\Index.html" $R0
; $R0="html"
SectionEnd

Section un.Install
${GetParent} "C:\My Downloads\Index.html" $R0
; $R0="C:\My Downloads"
SectionEnd
fitzroy_doll#
Edit: did not see the reply above before I posted. That sounds much better, though this works too:

Edit2: Actually the post above does not do what I want, but this does:

StrStrip seems to do the job...

If you start with

EXEDIR = C:\Program Files\my app\my folder\target folder\

then call this when you need it

${StrStrip} "$\my folder$\target folder" "$EXEDIR" $R0

this is the result:

$R0 = C:\Program Files\my app


Seems like a ropey way to do things, but I can't see a better way, unless someone can suggest something.
demiller9#
Your use of StrStrip requires that you know exactly what the path components are that have to be removed. Perhaps in your situation that is a constant and it will work for you. If that is the case, then just a simple string copy to remove X characters from the end is the easiest solution:
StrCpy $0 "C:\Program Files\my app\my folder\target folder\" -24
; $0 = "C:\Program Files\my app\"
In general, GetParent is the better way. Your example requires calling it twice since you want to move up two levels in the path:
${GetParent} "C:\Program Files\my app\my folder\target folder" $0
${GetParent} $0 $0
; $0 = "C:\Program Files\my app"
fitzroy_doll#
Ah, okay. Many thanks.

I've ended up in fact with ${GetParent} $EXEDIR $0, as I do know the nearby path components, but not the whole string, so it really depends on the installer being in the right place.
MSG#
Actually you can even do "C:\NonExistingDirectory\.." and you'd get "C:\" again. I just can't remember whether or not this also works on win9x.