The data from a file can be read to a 1 dimensional array
containing one line of text per element using FileReadToArray( "filehandle/filename" ) function. We can
pass either the file handle returned by a call to FileOpen() or the file name string to read the data.
The function will return the array of data on success and @extended
set to the number of lines read. Array UBound() function also can be used to
determine the number of lines read.
In the case of failure, sets @error flag to non-zero. 1 =
Error opening specified file and 2 = Empty file.
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 a set of lines for demonstration
FileWriteLine($hFileOpen, "This is the first line")
FileWriteLine($hFileOpen, "This is the second line")
FileWriteLine($hFileOpen, "This is the third line")
FileWriteLine($hFileOpen, "This is the last line")
;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)
;Read the data into an array using the file handle.
Local $aArray = FileReadToArray($hFileOpen)
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
Else
For $i = 0 To UBound($aArray) - 1 ; Loop through the array.
MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array.
Next
EndIf
;Close the handle returned by FileOpen.
FileClose($hFileOpen)
#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 a set of lines for demonstration
FileWriteLine($hFileOpen, "This is the first line")
FileWriteLine($hFileOpen, "This is the second line")
FileWriteLine($hFileOpen, "This is the third line")
FileWriteLine($hFileOpen, "This is the last line")
;Set the file position to beginning for reading the data from the beginning of the file.
FileSetPos($hFileOpen, 0, $FILE_BEGIN)
;Read the data into an array using the file handle.
Local $aArray = FileReadToArray($hFileOpen)
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
Else
For $i = 0 To UBound($aArray) - 1 ; Loop through the array.
MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) ; Display the contents of the array.
Next
EndIf
;Close the handle returned by FileOpen.
FileClose($hFileOpen)
- AutoIt - Read a specific line of data from a file
- AutoIt - Read 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