Write to text file line number: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 35: Line 35:
ClearErrors
ClearErrors
FileRead $4 $6
FileRead $4 $6
'''IfErrors  
IfErrors Exit
== Exit
  IntOp $3 $3 + 1
  IntOp $3 $3 + 1
  StrCmp $3 $1 0 +3
  StrCmp $3 $1 0 +3
FileWrite $5 "$2$\r$\n$6"
FileWrite $5 "$2$\r$\n$6" # THIS CODE ADD NEW TEXT IN LINE x WITHOUT REMOVING OLD TEXT INSTED ADD OLD TEXT IN NEW LINE
FileWrite $5 "$2$\r$\n"  # THIS WAY WILL REPLACE LINE x WITHOUT ADDING OLD TEXT IN NEW LINE
Goto Loop
Goto Loop
FileWrite $5 $6
FileWrite $5 $6
Goto Loop
Goto Loop
Exit:'''
Exit:
  FileClose ==
 
$5
  FileClose $5
  FileClose $4
  FileClose $4
'''''
 
SetDetailsPrint none'''''
SetDetailsPrint none
Delete $0
Delete $0
Rename $7 $0
Rename $7 $0

Latest revision as of 23:18, 11 June 2012

Author: Afrow UK (talk, contrib)


Description

This writes the input string text to a specified line number in a specified file, without deleting any existing data.

Usage

Push hello
Push 24
Push "$INSTDIR\file.txt"
Call WriteToFileLine

The Function

Function WriteToFileLine
Exch $0 ;file
Exch
Exch $1 ;line number
Exch 2
Exch $2 ;string to write
Exch 2
Push $3
Push $4
Push $5
Push $6
Push $7
 
 GetTempFileName $7
 FileOpen $4 $0 r
 FileOpen $5 $7 w
 StrCpy $3 0
 
Loop:
ClearErrors
FileRead $4 $6
IfErrors Exit
 IntOp $3 $3 + 1
 StrCmp $3 $1 0 +3
FileWrite $5 "$2$\r$\n$6" # THIS CODE ADD NEW TEXT IN LINE x WITHOUT REMOVING OLD TEXT INSTED ADD OLD TEXT IN NEW LINE
FileWrite $5 "$2$\r$\n"   # THIS WAY WILL REPLACE LINE x WITHOUT ADDING OLD TEXT IN NEW LINE 
Goto Loop
FileWrite $5 $6
Goto Loop
Exit:
 
 FileClose $5
 FileClose $4
 
SetDetailsPrint none
Delete $0
Rename $7 $0
SetDetailsPrint both
 
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd

-Stu