The data from a file can be read using the function
FileRead( "filehandle/filename" [, count] ). The data can be read by
passing either thehandle returned by call to FileOpen() or by passing the file name.The optional Count
parameter is the number of characters to read from the file. If not specified, it
reads the entire data from the file.
The function will return the binary/string read on success
and @extended is set to the number of bytes/characters returned.
In the case of failure, sets @error flag to non-zero. 1 = if
file not opened in read mode or other error. -1 = if end-of-file is reached.
Example:
#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 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 sample is from:" & @CRLF)
FileWrite($hFileOpen, "Automation Developers")
;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)
;Read the data from the file using the handle returned by FileOpen
Local $sFileRead = FileRead($hFileOpen)
;Close the handle returned by FileOpen.
FileClose($hFileOpen)
;Display the data.
MsgBox($MB_SYSTEMMODAL, "Automation Developers", $sFileRead)
#include <MsgBoxConstants.au3>
;Assign the file path to a variable
Local $sFilePath = "C:\AutomationDevelopers\temp.txt"
;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 sample is from:" & @CRLF)
FileWrite($hFileOpen, "Automation Developers")
;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)
;Read the data from the file using the handle returned by FileOpen
Local $sFileRead = FileRead($hFileOpen)
;Close the handle returned by FileOpen.
FileClose($hFileOpen)
;Display the data.
MsgBox($MB_SYSTEMMODAL, "Automation Developers", $sFileRead)
- AutoIt - Read data from a file to an array
- AutoIt - Read a specific line of data from 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 - Write a line of text to a file
- AutoIt - Write data to a file
- AutoIt - Open a file for read/ write operations
Interesting? Share and Let Others Know.
Post a Comment