Hi,
I noticed that if I entered bad characters(<>?🙂 in the installation directory MUI page, the $INSTDIR variable skips those bad characters. I want to warn user that they have entered an invalid path name, so is there a way to get the raw string from installation directory page? Thanks in advance.
Bad characters(<>?:) in $INSTDIR variable
4 posts
Figured it out, just do not use $INSTDIR to catch directory from MUI
I assigned $R1 instead
I assigned $R1 instead
!define MUI_DIRECTORYPAGE_VARIABLE $R1
Use StringFilter to filter these symbols: '/:*?"<>|'
After: D:\AB CD [{]\.E } ]\F\df .}.1. fff\ff f\1 2d ff]
This is the snapshot of an example using the code:

; Stores the first two symbols to $R1Before: D:\AB CD [{] / |\ ...\.E !@ / } <] \ / F \ \\\ \df .}.1. ///fff\ff ?f>\ 1 2d ff] \
StrCpy $R1 $INSTDIR 2
; Others to $INSTDIR
StrCpy $INSTDIR $INSTDIR "" 2
; {StrFilter} "[string]" "[options]" "[symbols1]" "[symbols2]" $var
; You can create your own rule by modifying the list of symbols include or exclude
; Delete illegal symbols from $INSTDIR
; Note there is a space after ')', for a blank space is valid in path
${StrFilter} $INSTDIR 12 '.\{}[]() ' '/:*?"<>|' $INSTDIR
; Replace continuous spaces with one space
${WordReplace} $INSTDIR ' ' ' ' '+*' $INSTDIR
${WordReplace} $INSTDIR ' ' ' ' '+' $INSTDIR
; Replace continuous dots with one dot
${WordReplace} $INSTDIR '..' '.' '+*' $INSTDIR
${WordReplace} $INSTDIR '..' '.' '+' $INSTDIR
; A dot at the end of a folder name is invalid
${WordReplace} $INSTDIR '.\' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '.\' '\' '+' $INSTDIR
; Replace continuous backsplashes with one backsplash
${WordReplace} $INSTDIR '\\' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '\\' '\' '+' $INSTDIR
; A space at the begening of a folder name is invalid
${WordReplace} $INSTDIR ' \' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR ' \' '\' '+' $INSTDIR
; A space at the end of a folder name is invalid
${WordReplace} $INSTDIR '\ ' '\' '+*' $INSTDIR
${WordReplace} $INSTDIR '\ ' '\' '+' $INSTDIR
; Join to full path
StrCpy $INSTDIR $R1$INSTDIR
After: D:\AB CD [{]\.E } ]\F\df .}.1. fff\ff f\1 2d ff]
This is the snapshot of an example using the code:
I think bensmatt was referring to $INSTDIR being one of the special internal variables that NSIS treats as a file path, which automatically strips illegal characters. The same way one might use...
I.e. execute the following, and try to enter the illegal path: c:\*.*
If instead you use:
This presents the developer with a problem if they want to warn the user about illegal characters, as they are already filtered before you can detect them.
Push $PathToClean
Exch $EXEDIR
Exch $EXEDIR
Pop $PathToClean
I.e. execute the following, and try to enter the illegal path: c:\*.*
The MessageBox will state that "$INSTDIR = c:\.". Thus you can't detect the asterisks having been entered. Note that these asterisks -are- still present in the actual directory text field, of course. So to the end-user, there doesn't seem to be anything amiss.
OutFile "test.exe"
Page Directory
Function .onVerifyInstDir
MessageBox MB_OK "$$INSTDIR = $INSTDIR"
FunctionEnd
Section
SectionEnd
If instead you use:
Then the behavior is more like what you might expect. You could then use that to adjust the path and warn the user; though more easily done from a custom page, the basic principles are the same.
OutFile "test.exe"
Var varInstDir
PageEx Directory
DirVar $varInstDir
PageExEnd
Function .onVerifyInstDir
MessageBox MB_OK "$$varInstDir = $varInstDir"
FunctionEnd
Section
SectionEnd