EnsureEndsWith
From NSIS Wiki
Jump to navigationJump to search
Here's a macro that I wrote, may be helpful to others. Sometimes when you get user input, it's important to ensure that it ends with a particular string (I.E. ".TXT" for a filename, or "\" for a path).
This macro ensures that the value in VALUE ends with the string END_WITH. If VALUE is "Test" and END_WITH is "\" then this will change VALUE to "Test\". On the other hand, if VALUE was "Test2\", it would be unchanged.
!MACRO EnsureEndsWith VALUE END_WITH Push $R0 Push $R1 Push $R2 StrLen $R0 ${END_WITH} ; how long is the ending string IntOp $R1 0 - $R0 ; how far to offset back from the end of the string StrCpy $R2 ${VALUE} $R0 $R1 ;take N chars starting N from the end of VALUE put in R2 StrCmp $R2 ${END_WITH} +2 ;if the last N chars = END_WITH, good. StrCpy ${VALUE} "${VALUE}${END_WITH}" ; otherwise, append END_WITH Pop $R2 Pop $R1 Pop $R0 !MACROEND