Simple write text to file

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This is a simple function to write a piece of text to a file. This will write to the end always.

Usage

Push `hello$\r$\n` ;text to write to file 
Push `$INSTDIR\log.txt` ;file to write to 
Call WriteToFile

Usage 2

${WriteToFile} `$INSTDIR\log.txt` `hello`
${WriteLineToFile} `$INSTDIR\log.txt` `hello`

The Function

Function WriteToFile
Exch $0 ;file to write to
Exch
Exch $1 ;text to write
 
  FileOpen $0 $0 a #open file
  FileSeek $0 0 END #go to end
  FileWrite $0 $1 #write to file
  FileClose $0
 
Pop $1
Pop $0
FunctionEnd
 
!macro WriteToFile NewLine File String
  !if `${NewLine}` == true
  Push `${String}$\r$\n`
  !else
  Push `${String}`
  !endif
  Push `${File}`
  Call WriteToFile
!macroend
!define WriteToFile `!insertmacro WriteToFile false`
!define WriteLineToFile `!insertmacro WriteToFile true`

-Stu