Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

Text.ReadTextFile

Reads a text file.

Component Version macOS Windows Linux Server iOS SDK
Text 2.4 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "Text.ReadTextFile"; FilePath { ; Encoding } )   More

(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")]

Read file as Base64 encoded:

MBS( "Text.ReadTextFile"; $FilePath; "base64" )

Change line endings for a text file:

Set Variable [ $path ; Value: "/Users/cs/Desktop/test.txt" ]
Set Variable [ $text ; Value: MBS("Text.ReadTextFile"; $path; "UTF-8") ]
Set Variable [ $text ; Value: MBS("Text.ReplaceNewline"; $text; 3)
// 1 = CR for Mac
// 2 = LF for Linux
// 3 = CRLF for Windows ]
Set Variable [ $r ; Value: MBS("Text.WriteTextFile"; $text; $path; "UTF-8") ]

See also

Release notes

  • Version 13.3

Example Databases

Blog Entries

This function is free to use.

Created 18th August 2014, last changed 2nd October 2023


Text.ReadEMLXFile - Text.ReadTextFromContainer