For example, if the user wants to install the app to:
C:\Program Files\ABC\123
and ABC doesn't exist in Program Files, the function will return C:\Program Files\ABC, it makes it usefull for deleting all the directories that you've created upon uninstall.
The function looks like:
; FindTopLevelDirectory
; input, none, uses $INSTDIR
; output, top of stack (replaces, with e.g. C:\Program Files)
; the top-most parent of $INSTDIR that does not currently exist
; modifies no other variables.
;
; Usage:
; Call FindTopLevelDirectory
; Pop $R0
; ; at this point $R0 will equal "C:\Program Files\NonexistantDirectory"
function FindTopLevelDirectory
; $R2 directory path to installation
; $R3 counter
; $R4 temp counter
; $R5 temp string
Pop $R2 ;the orginal string
StrCpy $R3 "-1"
StrCpy $R5 ""
Loop:
IntOp $R3 $R3 + 1
StrCpy $R4 $R2 1 $R3
StrCmp $R4 "" ExitLoop
StrCmp $R4 "\" Directory
StrCpy $R5 $R5$R4
Goto Loop
Directory:
StrCpy $R5 "$R5"
IfFileExists "$R5\*.*" "" ExitLoop
StrCpy $R5 "$R5\"
Goto Loop
ExitLoop:
IfFileExists "$R5*.*" +2
Push $R5
FunctionEnd The problem is when the path has Japanese characters, the statementStrCpy $R4 $R2 1 $R3 returns an invalid character, which is expected, because it's a multibyte character. I somtimes can detect this by doing the following instead: StrCpy $R4 $R2 1 $R3
StrCmp $R4 "" "" Check
; try 2 bytes in case it's a multibyte folder.
StrCpy $R4 $R2 2 $R3
StrCmp $R4 "" ExitLoop
Check:
StrCmp $R4 "\" Directory But sometimes the copying 1 character returns a valid character, even though it's part of a multibyte character, and I get what is commonly known as mojibake. Does anyone have any ideas of a better way to copy one character at a time that handles multibyte characters? Or is there a better way to iterate over the parent directories of a path?
I'm using NSIS 2.01b if that helps.