Topics
All
MacOS
(Only)
Windows
(Only)
Linux
(Only, Not)
iOS
(Only, Not)
Components
Crossplatform Mac & Win
Server
Client
Old
Deprecated
Guides
Examples
Videos
New in version:
11.0
11.1
11.2
11.3
11.4
11.5
12.0
12.1
12.2
12.3
Statistic
FMM
Blog
Text.ReadTextFile
Reads a text file.
Component | Version | macOS | Windows | Linux | Server | iOS SDK | License |
Text | 2.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | Paid |
(old name: String.ReadTextFile)
Parameters
Parameter | Description | Example | Flags |
---|---|---|---|
FilePath | The native file path. Something like "/Users/cs/desktop/test.txt" on Mac and "C:\Programs\Data\test.txt" on Windows. Files without path end in the root directory on Mac. | "test.txt" | |
Encoding | The text encoding for result. Default is native. This function can also handle UTF-16 as well as UTF-16LE and UTF-16BE for little/big endian byte order. Possible encoding names: ANSI, ISO-8859-1, Latin1, Mac, Native, UTF-8, DOS, Hex, Base64 or Windows. More listed in the FAQ. |
"UTF8" | Optional |
Result
Returns text on success and error on failure.
Description
Reads a text file.You have to decide which encoding to use (if not native). We support a few encodings, but we can add more if you need something special. Some characters will be wrong if the encoding is not correct.
See also Files.ReadJPEG, Files.ReadPDF, Files.ReadPNG, Files.ReadFile and RichText.ReadFile.
This function reads only up to 1 GB of text. And even than a 32-bit FileMaker may fail to allocate enough memory to handle the text.
When passing UTF-16 as encoding, we can detect byte order if there is a Byte Order Mark.
Use Container.GetText function to read text content from a file in container.
This function requires a native path. Use Path.FileMakerPathToNativePath to convert a FileMaker path to a native path if required. If you like to have the user choose the path, you can use FileDialog functions.
For Server be aware that server has limited permissions and may not be able to access all files on a computer.
Examples
Read system.log file:
MBS("Text.ReadTextFile"; "/var/log/system.log"; "utf8")
Read Windows ANSI XML:
# read and convert to from Windows ANSI to unicode
Set Variable [$text; Value:MBS("Text.ReadTextFile"; "/Users/cs/Desktop/test.xml"; "Windows")]
# remove the encoding hint:
Set Variable [$text; Value:Substitute($text; "<?xml version=\"1.0\" encoding=\"WINDOWS-1252\"?>"; "")]
# now xml is unicode text and can be passed to XML functions.
Read file on Windows from desktop folder:
MBS( "Text.ReadTextFile"; "C:\Users\Christian\Desktop\test.txt")
Read file with UTF-16 Big Endian:
MBS( "Text.ReadTextFile"; "/Users/cs/Desktop/test.txt" ; "UTF-16BE" )
Parse in VCard with MBS functions:
# Loop over files on desktop
Set Variable [ $folder ; Value: MBS( "Folders.UserDesktop" ) ]
Set Variable [ $files ; Value: MBS( "Files.List"; $folder; 1+4; ".vcf" ) ]
If [ MBS("IsError") = 0 ]
Set Variable [ $count ; Value: ValueCount ( $files ) ]
Set Variable [ $index ; Value: 1 ]
If [ $count > 0 ]
Loop
# Read in vcard
Set Variable [ $filename ; Value: GetValue($files; $index) ]
Set Variable [ $filepath ; Value: MBS( "Path.AddPathComponent"; $folder; $filename ) ]
Set Variable [ $text ; Value: MBS( "Text.ReadTextFile"; $FilePath; "UTF-8" ) ]
If [ MBS("IsError") = 0 ]
Set Variable [ $text ; Value: MBS( "Text.ReplaceNewline"; $text; 1) ]
# Process all text lines
Set Variable [ $LineCount ; Value: ValueCount ( $text ) ]
Set Variable [ $LineIndex ; Value: 1 ]
# TODO: Create new record here
Loop
Set Variable [ $Line ; Value: GetValue($text; $lineindex) ]
If [ Left ( $line ; 2 ) = "N:" ]
# Name
Set Variable [ $z ; Value: Middle ( $line ; 3; Length ( $line )) ]
Set Variable [ $list ; Value: MBS( "List.CSVSplit"; $z ) ]
Set Variable [ $FirstName ; Value: GetValue($list; 1) ]
Set Variable [ $SureName ; Value: GetValue($list; 2) ]
Set Field [ CON::nameGiven ; $firstName ]
Set Field [ CON::nameFamily ; $sureName ]
Else If [ Left($line; 6) = "EMAIL;" ]
# TODO
Else If [ Left($line; 27) = "PHOTO;ENCODING=b;TYPE=JPEG:" ]
# Photo as base64 coded JPEG:
Set Variable [ $data ; Value: Middle ( $line ; 28; Length ( $line )) ]
Set Variable [ $LineIndex ; Value: $LineIndex + 1 ]
Loop
Set Variable [ $Line ; Value: GetValue($text; $lineindex) ]
Exit Loop If [ Left ( $line ; 1 ) ≠ " " ]
Set Variable [ $data ; Value: $data & ¶ & $line ]
# next
Set Variable [ $LineIndex ; Value: $LineIndex + 1 ]
Exit Loop If [ $LineIndex > $LineCount ]
End Loop
Set Variable [ $LineIndex ; Value: $LineIndex - 1 ]
Set Variable [ $image ; Value: Base64Decode ( $data; "image.jpg" ) ]
Set Field [ CON::imageOrLogo ; $image ]
End If
# next
Set Variable [ $LineIndex ; Value: $LineIndex + 1 ]
Exit Loop If [ $LineIndex > $LineCount ]
End Loop
# TODO: Commit record
End If
# next
Set Variable [ $index ; Value: $index + 1 ]
Exit Loop If [ $index > $count ]
End Loop
End If
End If
Read and write file to change some text:
Set Variable [$path; Value: "/Users/cs/Desktop/test.txt" )]
# read the file
Set Variable [$text; Value:MBS( "Text.ReadTextFile"; $path; "UTF-8")]
# replace something in the text
Set Variable [$text; Value: Substitute($text; "Hello"; "World") ]
# write file
Set Variable [$r; Value:MBS( "Text.WriteTextFile"; $text; $path; "UTF-8")]
See also
- List.CSVSplit
- Path.AddPathComponent
- RunTask.Launch
- SendMail.SetRichText
- SyntaxColoring.AddContextMenuCommand
- Text.AppendTextFile
- Text.ReadEMLXFile
- Text.RTFToText
- XML.Import
- XML.Import.Work
Example Databases
- CURL/Email/IMAP Email
- Drag and Drop/for Mac/DragTest Email
- Utility functions/Split DDR
- Win Only/Snippets for Windows
- XML/XML Import
Blog Entries
- Import CSV with Matrix functions
- Split XML for DDR into files for GIT
- FileMaker 18 File Script Steps vs. BinaryFile functions
- Parsing VCard file
- MBS Filemaker Plugin, version 4.1pr1
- MBS Filemaker Plugin, version 3.2pr7
- MBS Filemaker Plugin, version 3.1pr8
- MonkeyBread Software Releases the MBS Filemaker plug-in in version 2.4
- MBS Filemaker Plugin 2.4 release notes
- MBS Filemaker Plugin, version 2.4pr8
FileMaker Magazin
Created 18th August 2014, last changed 28th January 2021
Text.ReadEMLXFile - Text.ReadTextFromContainer
Feedback: Report problem or ask question.
