LATEST UPDATES
ShareHolder Finder v0.9.3 is now released!!! Check Product Page for more details.
✉ info@automationdevelopers.com

ITS ALL ABOUT AUTOMATION

Friday, 26 February 2016

AutoIt - Write data 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"

;Write data to file by directly passing the file path. In case of error, display a message box.
;FileWrite returns 0 if file not opened in writemode, file is read only, or file cannot otherwise be written to.
If Not FileWrite($sFilePath, "This is the first line." & @CRLF) Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred while writing data to the file.")
EndIf

;Open the file temp.txt in append mode. If the folder C:\AutomationDevelopers does not exist, it will be created.
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND + $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.")
EndIf

;Write data to file by passing the previously opened file handle.
FileWrite($hFileOpen, "This is the second line.")

;Close the handle returned by FileOpen.
FileClose($hFileOpen)

We can write data to a file by passing the either the file name or by the handle returned by call to FileOpen(). When using file handle, 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.
FileWrite() will return 1 for success and 0 for failures.

Related Posts




Interesting? Share and Let Others Know.

Post a Comment