Old 7th April 2019, 13:20   #1
meoit
Senior Member
 
Join Date: Oct 2014
Location: $DESKTOP
Posts: 100
Smile How to find a double quotes string and replace it

I need find a double quotes string and replace it.

Example, in my .json file:

!define Find_Me '"abcdefg":{"hot":true'
!define Replace_Me '"zzxxyy":{"hot":false'

In the Find_Me and Replace_Me, there are double quotes strings.

Somebody help me.

Thanks.
meoit is offline   Reply With Quote
Old 7th April 2019, 13:35   #2
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 343
you must use StrRep Function:

Example:

HTML Code:

!define Find_Me '"abcdefg":{"hot":true'
!define Replace_Me '"zzxxyy":{"hot":false'

ShowInstDetails show


Var Final_String


!define StrRep "!insertmacro StrRep"
!macro StrRep output string old new
    Push `${string}`
    Push `${old}`
    Push `${new}`
    !ifdef __UNINSTALL__
        Call un.StrRep
    !else
        Call StrRep
    !endif
    Pop ${output}
!macroend
 
!macro Func_StrRep un
    Function ${un}StrRep
        Exch $R2 ;new
        Exch 1
        Exch $R1 ;old
        Exch 2
        Exch $R0 ;string
        Push $R3
        Push $R4
        Push $R5
        Push $R6
        Push $R7
        Push $R8
        Push $R9
 
        StrCpy $R3 0
        StrLen $R4 $R1
        StrLen $R6 $R0
        StrLen $R9 $R2
        loop:
            StrCpy $R5 $R0 $R4 $R3
            StrCmp $R5 $R1 found
            StrCmp $R3 $R6 done
            IntOp $R3 $R3 + 1 ;move offset by 1 to check the next character
            Goto loop
        found:
            StrCpy $R5 $R0 $R3
            IntOp $R8 $R3 + $R4
            StrCpy $R7 $R0 "" $R8
            StrCpy $R0 $R5$R2$R7
            StrLen $R6 $R0
            IntOp $R3 $R3 + $R9 ;move offset by length of the replacement string
            Goto loop
        done:
 
        Pop $R9
        Pop $R8
        Pop $R7
        Pop $R6
        Pop $R5
        Pop $R4
        Pop $R3
        Push $R0
        Push $R1
        Pop $R0
        Pop $R1
        Pop $R0
        Pop $R2
        Exch $R1
    FunctionEnd
!macroend
!insertmacro Func_StrRep ""
!insertmacro Func_StrRep "un."


Section

StrCpy '$Final_String' '${Find_Me}'
DetailPrint "$Final_String"

${StrRep} '$Final_String' '$Final_String' '${Find_Me}' '${Replace_Me}'
DetailPrint "$Final_String"

SectionEnd
r2du-soft is offline   Reply With Quote
Old 7th April 2019, 14:42   #3
meoit
Senior Member
 
Join Date: Oct 2014
Location: $DESKTOP
Posts: 100
I am sorry. Double quotes string [in a file]

In this file, have "abcdefg":{"hot":true

so, I try !define.....
!define Find_Me '"abcdefg":{"hot":true'
!define Replace_Me '"zzxxyy":{"hot":false'
meoit is offline   Reply With Quote
Old 7th April 2019, 17:32   #4
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 343
for replace string in file use like this:

HTML Code:
!include "NewTextReplace.nsh"


!define Find_Me '"abcdefg":{"hot":true'
!define Replace_Me '"zzxxyy":{"hot":false'

Var File_Path_and_Name


Section

StrCpy $File_Path_and_Name "$EXEDIR\TestFile.txt"

	${textreplace::FindInFile} "$File_Path_and_Name" '${Find_Me}' "/S=1" $0
	MessageBox MB_OK 'number of find the string is $0'

	${textreplace::ReplaceInFile} "$File_Path_and_Name" "$File_Path_and_Name" '${Find_Me}' '${Replace_Me}' "/S=1 /C=1 /AO=1" $0
	MessageBox MB_OK 'number of find the replace the string is $0'

	${textreplace::Unload}

SectionEnd

You must use the plugin textreplace
https://nsis.sourceforge.io/TextReplace_plugin
r2du-soft is offline   Reply With Quote
Old 8th April 2019, 00:15   #5
meoit
Senior Member
 
Join Date: Oct 2014
Location: $DESKTOP
Posts: 100
Thanks r2du-soft !

This is test image.





Test on Windows 10 v1809 x64.
meoit is offline   Reply With Quote
Old 8th April 2019, 00:24   #6
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 343
Are you use from unicode true in the you're codes??

HTML Code:
unicode true
r2du-soft is offline   Reply With Quote
Old 8th April 2019, 02:03   #7
meoit
Senior Member
 
Join Date: Oct 2014
Location: $DESKTOP
Posts: 100
Yes, I have inserted it before.
meoit is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump