Replacing Lines in a Text File: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category links.)
m (Adding new author and category links.)
Line 1: Line 1:
{|align=right
{{PageAuthor|KiCHiK}}
|<small>Author: [[{{ns:2}}:KiCHiK|KiCHiK]] ([[{{ns:3}}:KiCHiK|talk]], [[{{ns:-1}}:Contributions/KiCHiK|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
This script will replace every occurrence of the line "line to replace" with the line "replacement of line" in the file called "file.txt".
This script will replace every occurrence of the line "line to replace" with the line "replacement of line" in the file called "file.txt".
Line 29: Line 27:
   Delete $R0</highlight-nsis>
   Delete $R0</highlight-nsis>


[[{{ns:14}}:Text Files Manipulation Functions]]
[[Category:Text Files Manipulation Functions]]

Revision as of 13:43, 24 June 2005

Author: KiCHiK (talk, contrib)


Description

This script will replace every occurrence of the line "line to replace" with the line "replacement of line" in the file called "file.txt".

The Script

ClearErrors
FileOpen $0 "file.txt" "r"
GetTempFileName $R0
FileOpen $1 $R0 "w"
loop:
   FileRead $0 $2
   IfErrors done
   StrCmp $2 "line to replace$\r$\n" 0 +3
      FileWrite $1 "replacement of line$\r$\n"
      Goto loop
   StrCmp $2 "line to replace" 0 +3
      FileWrite $1 "replacement of line"
      Goto loop
   FileWrite $1 $2
   Goto loop
 
done:
   FileClose $0
   FileClose $1
   Delete "file.txt"
   CopyFiles /SILENT $R0 "file.txt"
   Delete $R0