Skip to content
⌘ NSIS Forum Archive

Using ${StrLoc} to find the first occurance index of a backslash from end of string

2 posts

prakashjv#

Using ${StrLoc} to find the first occurance index of a backslash from end of string

I am using ${StrLoc} method to find the first occurance index of a backslash [\] starting from end of string.

Syntax used: ${StrLoc} $2 "\\folder1\folder2\profile1\My Documents" "\" "<"

Output is 38 which is total length of the string. Where I am expecting it to return the \ index which is right before My Documents, counting from end of string.

I am not sure if I am missing something in using ${StrLoc} in a right way.

My need is to know the index of \ which is right before My Documents in the above string.

Any help is appriciated. Thanks !
Anders#
I personally never use these string functions from strfunc/wordfunc because I feel they are hard to use and in this case broken.

Part of the StrLoc code does "IntOp $R0 $R4 - $R6" and $6 is a string (most of the time) so the code makes no sense at all. The '<' direction mode should not be used in its current state. You should be able to write your own smaller and better version.

If Deguix is still around, maybe he can fix it...


Edit:

I whipped up some functions that might inspire you:

!define StrRLoc '!insertmacro StrRLoc'
!macro StrRLoc out str find
Push `${str}`
Push `${find}`
Call StrRLoc
Pop ${out}
!macroend

Function StrRLoc
Exch $3 ; Find
Exch
Exch $1 ; Str
Push $2 ; Idx
Push $4 ; Len($4)
Push $5 ; Temp
Push $6 ; Result
StrLen $4 $3
StrCpy $2 0
StrCpy $6 ""
loop:
StrCpy $5 $1 $4 $2
StrCmp $5 "" done
StrCmp $5 $3 0 more
StrCpy $6 $2
more:
IntOp $2 $2 + 1
Goto loop
done:
StrCpy $3 $6
Pop $6
Pop $5
Pop $4
Pop $2
Pop $1
Exch $3
FunctionEnd

Function StrRLocRevIdx
Exch $3 ; Find
Exch
Exch $1 ; Str
Push $2 ; -Idx
Push $4 ; Len($4)
Push $5 ; Temp
StrLen $4 $3
StrCpy $2 $4
loop:
StrCpy $5 $1 $4 -$2
StrCmp $5 "" stop
StrCmp $5 $3 match
IntOp $2 $2 + 1
Goto loop
stop:
StrCpy $3 ""
Goto ret
match:
IntOp $3 $2 - 1
ret:
Pop $5
Pop $4
Pop $2
Pop $1
Exch $3
FunctionEnd


Section
${StrRLoc} $2 "\\folder1\folder2\profile1\My Documents" "\"
DetailPrint |$2|
SectionEnd