ShareHolder Finder v0.9.3 is now released!!! Check Product Page for more details.


Friday, 26 February 2016

AutoIt - Write a line of text to a file

Author Post Date Friday, 26 February 2016

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

; Assign the file path to a variable
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"

;Open the file temp.txt in overwrite mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE + $FO_CREATEPATH)

;Display a message box in case of any errors.
If $hFileOpen = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when opening the file.")

;Write a line of data to file by passing the previously opened file handle. Newline (@CRLF) will be automatically added.
FileWriteLine($hFileOpen, "This is the first line.")
FileWriteLine($hFileOpen, "This is the second line.")

;Close the handle returned by FileOpen.

FileWriteLine will write a text of line to end of file. If the text of line does NOT end in @CR or @LF, then a DOS linefeed (@CRLF) will be automatically added. So that the next FileWriteLine operation will add text to next line.

FileWriteLine() returns 1 for Success and 0 for failures (if file not opened in writemode, file is read only, or file cannot otherwise be written to).

Related Posts

Interesting? Share and Let Others Know.

Post a comment