Replacing Lines in a Text File: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
m (Added category links.)
Line 28: Line 28:
   CopyFiles /SILENT $R0 "file.txt"
   CopyFiles /SILENT $R0 "file.txt"
   Delete $R0</highlight-nsis>
   Delete $R0</highlight-nsis>
[[{{ns:14}}:Text Files Manipulation Functions]]

Revision as of 20:44, 30 April 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