#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)
#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.
- AutoIt - Write a line of text to a file
- AutoIt - Write data to a specific line in a file (overwrite the existing line)
- AutoIt - Insert data to a specific line in a file
- AutoIt - Read data from a file
- AutoIt - Read a specific line of data from a file
- AutoIt - Read data from a file to an array
- AutoIt - Open a file for read/ write operations
Interesting? Share and Let Others Know.
Post a comment