Replace line that starts with specified string: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
mNo edit summary
Line 3: Line 3:
== Description ==
== Description ==
This function replaces lines with a string in a text file that start with a specified string.
This function replaces lines with a string in a text file that start with a specified string.
== Usage ==\
 
== Usage ==
<highlight-nsis>
<highlight-nsis>
  Push "$INSTDIR\file.ext" ; file to modify
  Push "$INSTDIR\file.ext" ; file to modify
  Push "string" ; string that a line must begin with
  Push "string" ; string that a line must begin with
  Push "string is blah" ; string to replace whole line with
  Push "string is blah" ; string to replace whole line with
Call ReplaceLineStr </highlight-nsis>
Call ReplaceLineStr
</highlight-nsis>
 
== The Function ==
== The Function ==
<highlight-nsis>
<highlight-nsis>
Line 69: Line 72:
  Pop $R0
  Pop $R0
FunctionEnd
FunctionEnd
</highlight-nsis> Written for [[user:moop]] in [http://forums.winamp.com/showthread.php?threadid=195615 this forum topic].
</highlight-nsis>
Written for [[user:moop]] in [http://forums.winamp.com/showthread.php?threadid=195615 this forum topic].
-Stu (Afrow UK)
-Stu (Afrow UK)


[[Category:Text Files Manipulation Functions]]
[[Category:Text Files Manipulation Functions]]

Revision as of 06:03, 4 December 2005

Author: Afrow UK (talk, contrib)


Description

This function replaces lines with a string in a text file that start with a specified string.

Usage

 Push "$INSTDIR\file.ext" ; file to modify
 Push "string" ; string that a line must begin with
 Push "string is blah" ; string to replace whole line with
Call ReplaceLineStr

The Function

Function ReplaceLineStr
 Exch $R0 ; string to replace that whole line with
 Exch
 Exch $R1 ; string that line should start with
 Exch
 Exch 2
 Exch $R2 ; file
 Push $R3 ; file handle
 Push $R4 ; temp file
 Push $R5 ; temp file handle
 Push $R6 ; global
 Push $R7 ; input string length
 Push $R8 ; line string length
 Push $R9 ; global
 
  StrLen $R7 $R1
 
  GetTempFileName $R4
 
  FileOpen $R5 $R4 w
  FileOpen $R3 $R2 r
 
  ReadLoop:
  ClearErrors
   FileRead $R3 $R6
    IfErrors Done
 
   StrLen $R8 $R6
   StrCpy $R9 $R6 $R7 -$R8
   StrCmp $R9 $R1 0 +3
 
    FileWrite $R5 "$R0$\r$\n"
    Goto ReadLoop
 
    FileWrite $R5 $R6
    Goto ReadLoop
 
  Done:
 
  FileClose $R3
  FileClose $R5
 
  SetDetailsPrint none
   Delete $R2
   Rename $R4 $R2
  SetDetailsPrint both
 
 Pop $R9
 Pop $R8
 Pop $R7
 Pop $R6
 Pop $R5
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Pop $R0
FunctionEnd

Written for user:moop in this forum topic. -Stu (Afrow UK)