Advanced Replace within text II: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Creating the temporary file in the same directory to keep the same access right as the original.)
 
Line 35: Line 35:
;Original Written by Afrow UK
;Original Written by Afrow UK
; Rewrite to Replace on line within text by rainmanx
; Rewrite to Replace on line within text by rainmanx
; Creating the temp file in the same directory by lars
; This version works on R4 and R3 of Nullsoft Installer
; This version works on R4 and R3 of Nullsoft Installer
; It replaces whatever is in the line throughout the entire text matching it.
; It replaces whatever is in the line throughout the entire text matching it.
Line 64: Line 65:
Push $R6 ;temp file name
Push $R6 ;temp file name
;-------------------------------
;-------------------------------
GetTempFileName $R6
; Find folder with file to edit:
GetFullPathName $R1 $0\..
; Put temporary file in same folder to preserve access rights:
GetTempFileName $R6 $R1
FileOpen $R1 $0 r ;file to search in
FileOpen $R1 $0 r ;file to search in
FileOpen $R0 $R6 w ;temp file
FileOpen $R0 $R6 w ;temp file

Latest revision as of 13:56, 18 December 2009

Author: rainmanp7 (talk, contrib)


Description

After many days of trial and error from a lot of texts.... Scripts.....
hehe

Written by Afrow UK 2003-05-18 12:51:22
Last updated by Afrow UK 2003-05-27 14:22:29

Modified By rainmanx
HI 2004-03-02 1:34PM

This script works as of Nullsoft r4 Feb 02 2004

I found by trial and error a script that worked for me for nullsoft r3 and r4 that looks into the entire file and replaces that exact text found with the text you supply and does not create BS lines and or move crap around. Afrow did an excellent job with this one, but it needed to work again in a special way for me so this is not my Original Work but my modification of Afrow's work :) thanx Afrow hehe

This replaces exactly the text given to replace and does not create extra crap
This is a Straight Replace 1 for 1 etc.

The Script

Push "C:\Program"             #-- text to be replaced  within the " "
Push "C:/Program"             #-- replace with anything within the " "
Push all                      #-- replace all occurrences 
Push all                      #-- replace all occurrences 
Push $INSTDIR\httpd.conf      #-- file to replace in 
Call AdvReplaceInFile         #-- Call the Function
 
;>>>>>> Function Junction BEGIN
;Original Written by Afrow UK
; Rewrite to Replace on line within text by rainmanx
; Creating the temp file in the same directory by lars
; This version works on R4 and R3 of Nullsoft Installer
; It replaces whatever is in the line throughout the entire text matching it.
Function AdvReplaceInFile
Exch $0 ;file to replace in
Exch
Exch $1 ;number to replace after
Exch
Exch 2
Exch $2 ;replace and onwards
Exch 2
Exch 3
Exch $3 ;replace with
Exch 3
Exch 4
Exch $4 ;to replace
Exch 4
Push $5 ;minus count
Push $6 ;universal
Push $7 ;end string
Push $8 ;left string
Push $9 ;right string
Push $R0 ;file1
Push $R1 ;file2
Push $R2 ;read
Push $R3 ;universal
Push $R4 ;count (onwards)
Push $R5 ;count (after)
Push $R6 ;temp file name
;-------------------------------
; Find folder with file to edit:
GetFullPathName $R1 $0\..
; Put temporary file in same folder to preserve access rights:
GetTempFileName $R6 $R1
FileOpen $R1 $0 r ;file to search in
FileOpen $R0 $R6 w ;temp file
StrLen $R3 $4
StrCpy $R4 -1
StrCpy $R5 -1
loop_read:
ClearErrors
FileRead $R1 $R2 ;read line
IfErrors exit
StrCpy $5 0
StrCpy $7 $R2
loop_filter:
IntOp $5 $5 - 1
StrCpy $6 $7 $R3 $5 ;search
StrCmp $6 "" file_write2
StrCmp $6 $4 0 loop_filter
StrCpy $8 $7 $5 ;left part
IntOp $6 $5 + $R3
StrCpy $9 $7 "" $6 ;right part
StrLen $6 $7
StrCpy $7 $8$3$9 ;re-join
StrCmp -$6 $5 0 loop_filter
IntOp $R4 $R4 + 1
StrCmp $2 all file_write1
StrCmp $R4 $2 0 file_write2
IntOp $R4 $R4 - 1
IntOp $R5 $R5 + 1
StrCmp $1 all file_write1
StrCmp $R5 $1 0 file_write1
IntOp $R5 $R5 - 1
Goto file_write2
file_write1:
FileWrite $R0 $7 ;write modified line
Goto loop_read
file_write2:
FileWrite $R0 $7 ;write modified line
Goto loop_read
exit:
FileClose $R0
FileClose $R1
SetDetailsPrint none
Delete $0
Rename $R6 $0
Delete $R6
SetDetailsPrint both
;-------------------------------
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd
;>>>>>>>>>>>>> Function END