Skip to content
⌘ NSIS Forum Archive

Read, edit a single line in a file?

5 posts

Morphie#

Read, edit a single line in a file?

Is it possible for NSIS to edit a single line in a file? Like overwrite line 2 of runmefirst.bat with "move ..\setup\*.* .\setup32"
kichik#
You will have to read the file using FileRead line by line and write to another temporary file, using FileWrite. When you reach the desired line write the line you want to replace it instead of the line read. After you write your line, continue reading and writing like before. Then just replace the old file with the temporary file.
kichik#
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
   FileWrite $1 $2
   Goto loop
done:
   FileClose $0
   FileClose $1
   Delete "file.txt"
   CopyFiles /SILENT $R0 "file.txt"
   Delete $R0