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

ITS ALL ABOUT AUTOMATION

Wednesday, 23 March 2016

AutoIt - Read data from a file


Author Post Date Wednesday, 23 March 2016
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)

Related Posts




Interesting? Share and Let Others Know.

Post a Comment