Read from Data File
Reads data from an open data file.
Options
- File ID is a numeric expression that specifies the file ID of an open data file. See Open Data File script step.
- Amount specifies the number of bytes to read if Read as is UTF-8 or Bytes. For UTF-16, Amount is the number of Unicode code units to read. If Amount is not specified (set to nothing), the entire file is read.
- Target is the field or variable to store the data read from the file.
- Read as specifies the character encoding of the file.
- UTF-16 is FileMaker’s default 16-bit Unicode (UTF-16 little endian) character encoding.
- UTF-8 is 8-bit Unicode character encoding.
- Bytes assumes no specific character encoding, but instead reads one byte at a time. If the target is a variable or container field, data read from the file is stored as container data with the same filename as the specified file.
Compatibility
Product | Supported |
FileMaker Pro | Yes |
FileMaker Go | Yes |
FileMaker WebDirect | No |
FileMaker Server | Yes |
FileMaker Cloud | Yes |
FileMaker Data API | No |
Custom Web Publishing | Yes |
Originated in version
18.0
Description
This script step reads data specified by Amount starting at the current read-write position, or it reads the entire file if Amount is not specified. See Set Data File Position.
The maximum amount of data this script step can read at a time is 64 MB. If a file is larger than 64 MB, you can perform this script step multiple times, reading an amount no larger than 64 MB each time.
Notes
- Performance is best when reading no more than 64 KB.
- When reading Unicode text from a portion of a file, only part of a character may be read if the character consists of multiple code units. If you expect to read text from a UTF-8 or UTF-16 file, it may be safer to read the entire file at one time unless you are sure of the number of code points per character.
Example 1
Reads the contents of an open file with a file ID of 2 and stores the data in a variable.
Read from Data File [ File ID: 2 ; Amount (bytes): ; Target: $variable ; Read as: Bytes ]
Example 2
Checks whether a file named change.log exists in the Documents folder. If it does, reads the first 100 UTF-8 code units into the Utilities::Log Entry text field.
Set Variable [ $file ; Value: Get ( DocumentsPath ) & "change.log" ]
Get File Exists [ "$file" ; Target: $fileExists ]
If [ not $fileExists ]
Exit Script [ Text Result: ]
End If
Open Data File [ "$file" ; Target: $fileID ]
Read from Data File [ File ID: $fileID ; Amount (bytes): 100 ; Target: Utilities::Log Entry ; Read as: UTF-8 ]
Close Data File [ File ID: $fileID ]
Example 3
If the large.log file exists and is large, reads the file 64 MB at a time into a variable until the entire file is read. If the file is smaller, it reads the entire file in one read operation. Data is read into a variable for improved performance, then the data is stored in a field.
Set Error Capture [ On ]
Set Variable [ $k_FileMissingError ; Value: 100 ]
Set Variable [ $k_EndOfFileError ; Value: 10 ]
Set Variable [ $k_64KB ; Value: 64 * 1024 ]
Set Variable [ $k_64MB ; Value: 64 * 1024 * 1024 ]
Set Variable [ $file ; Value: "large.log" ]
Get File Exists [ "$file" ; Target : $fileExists ]
If [ $fileExists = 0 ]
Exit Script [ Result: $k_FileMissingError ]
End If
Open Data File [ "$file" ; Target: $fileID ]
#If the file opened successfully, read the contents.
If [ Get ( LastError ) = 0 ]
Get File Size [ "$file" ; Target: $fileSize ]
#If the file size is greater than 64 KB, read it 64 MB at a time.
If [ $fileSize > $k_64KB ]
Loop
#Read up to 64 MB and store it in a variable.
Read from Data File [ File ID: $fileID ; Amount (bytes): $k_64MB ; Target: $dataChunk ; Read as: UTF-8 ]
Set Variable [ $readError; Value:Get ( LastError ) ]
#If the read operation was sucessful or if the end of the file was reached, concatenate the data read in this pass ($dataChunk) with the data read previously ($fileContents).
If [ ( $readError = 0 ) or ( $readError = $k_EndOfFileError ) ]
Set Variable [ $fileContents ; Value: $fileContents & $dataChunk ]
End If
#Exit the loop if the read operation failed or the end of the file is reached.
Exit Loop If [ $readError ]
End Loop
Else
#If the file is no larger than 64 KB, read it all at once.
Read from Data File [ File ID: $fileID ; Target: $fileContents ; Read as: UTF-8 ]
End If
#Close the data file and store the contents in a field.
Close Data File [ File ID: $fileID ]
Set Field [ Table::Data ; $fileContents ]
End If