Skip to content
⌘ NSIS Forum Archive

WordReplace - replace only one occurrence

5 posts

ChocJunkie#

WordReplace - replace only one occurrence

Hi,

I would like to replace the colons of a time string usinh 'h' for the first, 'm' for the second and 's' for the third colon.
If tryed using

${WordReplace} "$R1" ":" "h" "E+1" $R1
If the input is "10:20:30" the output is "10h2030".
My question is: How can I get 10h20m30s from the input?

Thanks 🙂

CJ
MSG#
I think this should be it:

!macro _ReplaceFirstByteOccurance _outvar _input _orig _replace
  Push $R0   ;max loopcounter
  Push $R1   ;loopcounter
  Push $R2
  !define Lprefix L${__LINE__}
  StrLen $R0 ${_input}
  StrCpy $R1 0
  ${Lprefix}Loop:
    StrCpy $R2 ${_input} 1 $R1
    StrCmp $R2 ${_orig} 0 ${Lprefix}NoMatch
      StrCpy $R2 ${_input} $R1
      StrCpy $R2 $R2${_replace}
      IntOp $R1 $R1 + 1
      StrCpy $R0 ${_input} "" $R1
      StrCpy $R2 $R2$R0
      goto ${Lprefix}End
    ${Lprefix}NoMatch:
    IntOp $R1 $R1 + 1
    StrCmp $R1 $R0 0 ${Lprefix}loop
    StrCpy $R2 ${_input}  ;nothing found, return original string.
  ${Lprefix}End:
  !undef Lprefix
  Push $R2
  Exch 3
  pop $R0
  pop $R2
  pop $R1
  pop ${_outvar}
!macroend
!define ReplaceFirstByteOccurance `!insertmacro _ReplaceFirstByteOccurance`
${ReplaceFirstByteOccurance} $1 $1 ":" "h"
${ReplaceFirstByteOccurance} $1 $1 ":" "m"
StrCpy $1 "$1s" 
Learn to use StrCmp/StrCpy/StrLen etc. They're very helpful commands.


Edit2: Doh, can't use $R1 as parameter to a macro that uses $R1, obviously.
ChocJunkie#
Thanks for your solution/hint! 🙂
I've got an idea now, how to implement my what I'm thinking about. ^^

Just because I'm curious:
So it's not possible using WordReplace for this!?
I understood its functionality for problems like this in first place... -.-

CJ
MSG#
Oh, I'm sure it can be done with WordReplace or some other macro like it. But why use a bulky general-purpose script when a tiny little macro of your own will suffice?
kichik#
WordReplace is good enough. I don't see how the code you've posted would result in the wrong output. This works fine for me.
StrCpy $R1 20:30:10
${WordReplace} "$R1" ":" "h" "E+1" $R1
DetailPrint $R1 # 20h30:10
${WordReplace} "$R1" ":" "m" "E+1" $R1
DetailPrint $R1 # 20h30m10