Simple write text to file: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Afrow UK}}
|<small>Author: [[{{ns:2}}:Afrow UK|Afrow UK]] ([[{{ns:3}}:Afrow UK|talk]], [[{{ns:-1}}:Contributions/Afrow UK|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
This is a simple function to write a piece of text to a file.
This is a simple function to write a piece of text to a file.
This will write to the end always.
This will write to the end always.
== Usage == <highlight-nsis>
== Usage ==
Push "hello$\r$\n" ;text to write to file  
<highlight-nsis>
Push "$INSTDIR\log.txt" ;file to write to  
Push `hello$\r$\n` ;text to write to file  
Push `$INSTDIR\log.txt` ;file to write to  
Call WriteToFile
Call WriteToFile
</highlight-nsis>
</highlight-nsis>


== Usage 2 == <highlight-nsis>
== Usage 2 ==
${WriteToFile} "hello$\r$\n" "$INSTDIR\log.txt"
<highlight-nsis>
${WriteToFile} `$INSTDIR\log.txt` `hello`
${WriteLineToFile} `$INSTDIR\log.txt` `hello`
</highlight-nsis>
</highlight-nsis>


== The Function ==
== The Function ==
<highlight-nsis> Function WriteToFile
<highlight-nsis>
Exch $0 ;file to write to
Function WriteToFile
Exch
Exch $0 ;file to write to
Exch $1 ;text to write
Exch
Exch $1 ;text to write


   FileOpen $0 $0 a #open file
   FileOpen $0 $0 a #open file
  FileSeek $0 0 END #go to end
  FileSeek $0 0 END #go to end
  FileWrite $0 $1 #write to file
  FileWrite $0 $1 #write to file
   FileClose $0
   FileClose $0


Pop $1
Pop $1
Pop $0
Pop $0
FunctionEnd
FunctionEnd


!macro WriteToFile String File
!macro WriteToFile NewLine File String
Push "${String}"
  !if `${NewLine}` == true
Push "${File}"
  Push `${String}$\r$\n`
  !else
  Push `${String}`
  !endif
  Push `${File}`
   Call WriteToFile
   Call WriteToFile
!macroend
!macroend
!define WriteToFile "!insertmacro WriteToFile"
!define WriteToFile `!insertmacro WriteToFile false`
</highlight-nsis> -Stu
!define WriteLineToFile `!insertmacro WriteToFile true`
</highlight-nsis>
-Stu
 
[[Category:Text Files Manipulation Functions]]

Latest revision as of 18:59, 14 September 2011

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