Write text/data to the end of a previously opened file.
FileWrite ( "filehandle/filename", "text/data" )
|filehandle/filename||The handle of a file, as returned by a previous call to FileOpen(). Alternatively, you may use a string filename as the first parameter.|
|text/data||The text/data to write to the file. The text is written as is - no @CR or @LF characters are added. See remark for data type.|
|Failure:||0 if file not opened in writemode, file is read only, or file cannot otherwise be written to.|
The file must be opened in write mode or the FileWrite() command will fail.
If a filename is given rather than a file handle, the file will be opened and closed during the function call. For parsing large text files this will be much slower than using filehandles. However, filename will be created if it does not already exist.
Note: Do not mix filehandles and filenames, i.e., don't FileOpen() a file and then use a filename in this function. Either use filehandles or filenames in your routines, not both.
When writing text AutoIt will write using UTF8 (without BOM) by default. To write in another mode the file must be opened with FileOpen() and the relevant flags.
If the data is a binary type variant (and not text) it will be written to the file byte by byte. Binary operation can also be forced by using FileOpen() with the binary flag.
Binary, FileFlush, FileGetPos, FileOpen, FileRead, FileReadLine, FileSetPos, FileWriteLine
#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir) ; Create a temporary file to write data to. If Not FileWrite($sFilePath, "Start of the FileWrite example, line 1. " & @CRLF) Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") Return False EndIf ; Open the file for writing (append to the end of a file) and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") Return False EndIf ; Write data to the file using the handle returned by FileOpen. FileWrite($hFileOpen, "Line 2") FileWrite($hFileOpen, "This is still line 2 as a new line wasn't appended to the last FileWrite call." & @CRLF) FileWrite($hFileOpen, "Line 3" & @CRLF) FileWrite($hFileOpen, "Line 4") ; Close the handle returned by FileOpen. FileClose($hFileOpen) ; Display the contents of the file passing the filepath to FileRead instead of a handle returned by FileOpen. MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & FileRead($sFilePath)) ; Delete the temporary file. FileDelete($sFilePath) EndFunc ;==>Example