Text.ReadTextFile
-----------------

Reads a text file.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [Text](component_Text.md) | [2.4](newinversion24.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "Text.ReadTextFile"; FilePath { ; Encoding } ) 

**MBS**( **"Text.ReadTextFile";** /\* Reads a text file. \*/  
**$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.e.g. "test.txt" \*/   
**$Encoding**) /\* Optional; 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.e.g. "UTF8" \*/ 

(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](https://www.monkeybreadsoftware.com/filemaker/faq.shtml). | "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](FilesReadJPEG.md), [Files.ReadPDF](FilesReadPDF.md), [Files.ReadPNG](FilesReadPNG.md), [Files.ReadFile](FilesReadFile.md) and [RichText.ReadFile](RichTextReadFile.md).  
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](ContainerGetText.md) function to read text content from a file in container.  
  
This function requires a native path. Use [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md) 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](component_FileDialog.md) 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 ; "&lt;?xml version=\\"1.0 \\" encoding=\\"WINDOWS-1252\\"?&gt;"; "")\]  
\# now xml is unicode text and can be passed to [XML](component_XML.md) 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](FoldersUserDesktop.md)" ) \]   
Set Variable \[ $files ; Value: MBS ( "[Files.List](FilesList.md)"; $folder ; 1+4; ".vcf" ) \]   
If \[ MBS ("IsError") = 0 \]   
 Set Variable \[ $count ; Value: ValueCount ( $files ) \]   
 Set Variable \[ $index ; Value: 1 \]   
 If \[ $count &gt; 0 \]   
 Loop  
 # Read in vcard  
 Set Variable \[ $filename ; Value: GetValue($files ; $index ) \]   
 Set Variable \[ $filepath ; Value: MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $folder ; $filename ) \]   
 Set Variable \[ $text ; Value: MBS ( "Text.ReadTextFile"; $FilePath ; "UTF-8" ) \]   
 If \[ MBS ("IsError") = 0 \]   
 Set Variable \[ $text ; Value: MBS ( "[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $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](ListCSVSplit.md)"; $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 &amp; ¶ &amp; $line \]   
 # next  
 Set Variable \[ $LineIndex ; Value: $LineIndex + 1 \]   
 Exit Loop If \[ $LineIndex &gt; $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 &gt; $LineCount \]   
 End Loop  
 # TODO: Commit record  
 End If  
 # next  
 Set Variable \[ $index ; Value: $index + 1 \]   
 Exit Loop If \[ $index &gt; $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](TextWriteTextFile.md)"; $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](TextReplaceNewLine.md)"; $text ; 3) \]  
\# 1 = CR for Mac   
\# 2 = LF for Linux   
\# 3 = CRLF for Windows   
Set Variable \[ $r ; Value: MBS ("[Text.WriteTextFile](TextWriteTextFile.md)"; $text ; $path ; "UTF-8") \]  
Read some statistics on Linux:

 Set Variable \[ $meminfo ; Value: MBS ("Text.ReadTextFile"; "/proc/meminfo") \]  
Set Variable \[ $loadavg ; Value: MBS ("Text.ReadTextFile"; "/proc/loadavg") \]  
Set Variable \[ $stat ; Value: MBS ("Text.ReadTextFile"; "/proc/stat") \]  
Count number of lines in a file:

 Let(\[  
 path = "/Users/cs/test.shtml";  
 text = MBS ( "Text.ReadTextFile"; path; "utf-8");  
 text = MBS ( "[Text.ReplaceNewLine](TextReplaceNewLine.md)"; text; 1);  
 count = ValueCount ( text )  
\]; count)  
Read server version:

 MBS ( "Text.ReadTextFile"; "/opt/FileMaker/FileMaker Server/Web Publishing/publishing-engine/node-wip/buildVersion.json"; "UTF-8" )  
<small>  
Example result:   
{ "buildDate" : "03/11/2026" , "buildVersion" : "26.0.1.43" } </small>### See also

- [Path.AddPathComponent](PathAddPathComponent.md)
- [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md)
- [SendMail.SetPlainText](SendMailSetPlainText.md)
- [SyntaxColoring.AddContextMenuCommand](SyntaxColoringAddContextMenuCommand.md)
- [Text.AppendTextFile](TextAppendTextFile.md)
- [Text.ReadEMLXFile](TextReadEMLXFile.md)
- [Text.ReplaceNewLine](TextReplaceNewLine.md)
- [Text.RTFToText](TextRTFToText.md)
- [XML.Import.Work](XMLImportWork.md)
- [XML.Validate](XMLValidate.md)

### Release notes

- **Version 16.1**
    - Improved [Text.ReadTextFile](https://www.mbsplugins.eu/TextReadTextFile.shtml) and other functions to read files on Windows instead of complaining about file in use.
- **Version 15.1**
    - Improved [Text.ReadTextFile](https://www.mbsplugins.eu/TextReadTextFile.shtml) to read things from /proc/ folder on Linux.
- **Version 13.3**
    - Fixed a problem with [Text.ReadTextFile](https://www.mbsplugins.eu/TextReadTextFile.shtml) reporting an error for zero byte files.

### Example Databases

- [CURL/Email/IMAP Email](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/Email/IMAP%20Email.shtml#12ScriptAnchor_)
- [Drag and Drop/for Mac/DragTest Email](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Drag%20and%20Drop/for%20Mac/DragTest%20Email.shtml#3ScriptAnchor_)
- [DynaPDF/ZUGFeRD and Facture-X/ZUGFeRD Validation](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/DynaPDF/ZUGFeRD%20and%20Facture-X/ZUGFeRD%20Validation.shtml#1ScriptAnchor_)
- [Saxon/Saxon Visualize electronic Invoices](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Saxon/Saxon%20Visualize%20electronic%20Invoices.shtml#4ScriptAnchor_)
- [Saxon/Saxon ZUGFeRD Validation](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Saxon/Saxon%20ZUGFeRD%20Validation.shtml#1ScriptAnchor_)
- [Shell/RunTask/RunTask](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Shell/RunTask/RunTask.shtml#1ScriptAnchor_)
- [Utility functions/Split DDR](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Utility%20functions/Split%20DDR.shtml#1ScriptAnchor_)
- [Win Only/Snippets for Windows](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Win%20Only/Snippets%20for%20Windows.shtml#11ScriptAnchor_)
- [XML/XML Import](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XML/XML%20Import.shtml#1ScriptAnchor_)

### Blog Entries

- [Validate electronic invoices with Saxon](https://www.mbsplugins.de/archive/2026-03-20/Validate_electronic_invoices_w/monkeybreadsoftware_blog_filemaker)
- [Visualize electronic invoices with Saxon](https://www.mbsplugins.de/archive/2026-03-16/Visualize_electronic_invoices_/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 16.1pr3](https://www.mbsplugins.de/archive/2026-02-16/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Validate electronic invoices with Saxon](https://www.mbsplugins.de/archive/2025-02-18/Validate_electronic_invoices_w/monkeybreadsoftware_blog_filemaker)
- [Visualize electronic invoices with Saxon](https://www.mbsplugins.de/archive/2025-02-17/Visualize_electronic_invoices_/monkeybreadsoftware_blog_filemaker)
- [Read system information on Linux](https://www.mbsplugins.de/archive/2025-02-07/Read_system_information_on_Lin/monkeybreadsoftware_blog_filemaker)
- [Validating schema for electronic invoices with ZUGFeRD](https://www.mbsplugins.de/archive/2024-10-11/Validating_schema_for_electron/monkeybreadsoftware_blog_filemaker)
- [How to use Trace](https://www.mbsplugins.de/archive/2023-07-15/How_to_use_Trace/monkeybreadsoftware_blog_filemaker)
- [CSV in FileMaker with MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2022-10-11/CSV_in_FileMaker_with_MBS_File/monkeybreadsoftware_blog_filemaker)
- [Import CSV with Matrix functions](https://www.mbsplugins.de/archive/2020-11-20/Import_CSV_with_Matrix_functio/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2018, Seite 25](https://filemaker-magazin.de/neuigkeit/4011-Appetithappen-FMM_201805)
- [Ausgabe 5/2011, Seite 55](https://filemaker-magazin.de/neuigkeit/3506-Appetithappen-FMM_201105-06)

This function is free to use.

Created 18th August 2014 , last changed 6th May 2026

  
[Text.ReadEMLXFile](TextReadEMLXFile.md) - [Text.ReadTextFromContainer](TextReadTextFromContainer.md)

[HTML Version](TextReadTextFile.shtml)