Text.WriteTextFile
------------------

Writes a text file with the given text.

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

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

**MBS**( **"Text.WriteTextFile";** /\* Writes a text file with the given text. \*/  
**$Text**; /\* The text to save.e.g. "Hello World" \*/   
**$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 text parameter.   
Default is native.e.g. "UTF-8" \*/ 

(old name: String.WriteTextFile)

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| Text | The text to save. | "Hello World" |  |
| 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 text parameter.    Default is native.   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). | "UTF-8" | Optional |

### Result

Returns "OK" or an error message.

### Description

Writes a text file with the given text.  
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.  
Please use [Text.ReplaceNewLine](TextReplaceNewLine.md) to define which line endings the file has.  
In order to append to a text file, you can use [Text.AppendTextFile](TextAppendTextFile.md) instead.  
If text encoding doesn't support given characters, they are converted to close match if possible or replaced by question marks.  
  
Existing file is overwritten if permissions allow it.  
  
Added support for UTF-16 in plugin version 7.2. You can pass UTF-16, UTF-16LE or UTF-16BE for encoding.   
  
See [Text.ReadTextFile](TextReadTextFile.md) for reading text files.  
If you need to create directory first, please use [Files.CreateDirectory](FilesCreateDirectory.md) function.  
  
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

Write UTF-8 file with some text:

 MBS ("Text.WriteTextFile"; $Text ; $Path ; "utf-8" )  
Write a test text file on Mac:

 MBS ("Text.WriteTextFile"; "Hello World. Some umlauts: äöü"; "/Users/cs/Desktop/testfile.txt"; "native")  
Write text in UTF-8 with Byte Order Mark (BOM):

 MBS ("Text.WriteTextFile"; Char(65279) &amp; "Hello World"; $Path ; "utf-8")  
Write a test text file on Win:

 MBS ("Text.WriteTextFile"; "Hello World. Some umlauts: äöü"; "c:\\test\\testfile.txt"; "native")  
Write RTF file:

 Set Variable \[$rtf ; Value:MBS ( "[Text.TextToRTF](TextTextToRTF.md)"; "Hello World äöü ß" )\]  
Set Variable \[$r ; Value:MBS ( "Text.WriteTextFile"; $rtf ; "/Users/cs/Desktop/test.rtf"; "Windows")\]  
Export data via SQL to text file:

 Set Variable \[$sql ; Value:"SELECT \\"Item\\", \\"Model\\", \\"Serial Number\\" FROM \\"Assets\\""\]  
Set Variable \[$text ; Value:MBS ( "[FM.ExecuteSQL](FMExecuteSQL.md)"; $sql ; 9; 13 )\]  
Set Variable \[$r ; Value:MBS ( "Text.WriteTextFile"; $text ; "/tmp/test.txt"; "UTF-8" )\]  
Write UTF-16 with LE, BE or default with BOM:

 MBS ("Text.WriteTextFile"; Char(65279) &amp; "Hello World"; "/Users/cs/Desktop/test UTF-16LE.txt"; "UTF-16LE")  
  
MBS ("Text.WriteTextFile"; Char(65279) &amp; "Hello World"; "/Users/cs/Desktop/test UTF-16BE.txt"; "UTF-16BE")  
  
MBS ("Text.WriteTextFile"; Char(65279) &amp; "Hello World"; "/Users/cs/Desktop/test UTF-16.txt"; "UTF-16")  
Write UTF-8 CRLF file:

 Set Variable \[ $text ; Value: "Joe,1234 Main St.¶John Glen,3132 Elm¶Johnson,5233 Wreybird¶Ray,263 Birch¶äöü" \]   
Set Variable \[ $path ; Value: "/Users/cs/Desktop/test.txt" \]   
Set Variable \[ $text ; Value: MBS ( "[Text.ReplaceNewLine](TextReplaceNewLine.md)"; $text ; 3) // CRLF \]   
Set Variable \[ $r ; Value: MBS ( "Text.WriteTextFile"; $text ; $path ; "UTF8" ) \]  
Write a HTML file and an image file to load in web viewer:

 Set Variable \[ $folder ; Value: MBS ( "[Folders.UserTemporary](FoldersUserTemporary.md)" ) \]   
Set Variable \[ $path ; Value: MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $folder ; "test.jpg" ) \]   
Set Variable \[ $r ; Value: MBS ( "[Container.WriteFile](ContainerWriteFile.md)"; Images::Image ; $path ) \]   
\#   
Set Variable \[ $html ; Value: "&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello World&lt;/p&gt;&lt;img src=test.jpg width=300&gt;&lt;/body&gt;&lt;/html&gt;" \]   
Set Variable \[ $path ; Value: MBS ( "[Path.AddPathComponent](PathAddPathComponent.md)"; $folder ; "test.html" ) \]   
Set Variable \[ $r ; Value: MBS ( "Text.WriteTextFile"; $html ; $path ) \]   
\#   
Set Variable \[ $URL ; Value: MBS ( "[Path.FilePathToFileURL](PathFilePathToFileURL.md)"; $Path ) \]   
Set Web Viewer \[ Object Name: "web" ; URL: $URL \]  
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](TextReadTextFile.md)"; $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

- [HotKey.Register](HotKeyRegister.md)
- [HotKey.SetEvaluate](HotKeySetEvaluate.md)
- [JSON.ToCSV](JSONToCSV.md)
- [Matrix.CSV](MatrixCSV.md)
- [Menubar.ListMenuCommands](MenubarListMenuCommands.md)
- [Path.AddPathComponent](PathAddPathComponent.md)
- [Path.FileMakerPathToNativePath](PathFileMakerPathToNativePath.md)
- [Plugin.LimitFunction](PluginLimitFunction.md)
- [Text.ReadTextFile](TextReadTextFile.md)
- [Text.ReplaceNewLine](TextReplaceNewLine.md)

### Release notes

- **Version 12.2**
    - Changed [Text.AppendTextFile](https://www.mbsplugins.eu/TextAppendTextFile.shtml), [Text.WriteToContainer](https://www.mbsplugins.eu/TextWriteToContainer.shtml) and [Text.WriteTextFile](https://www.mbsplugins.eu/TextWriteTextFile.shtml) to allow writing BOM character.
- **Version 7.2**
    - Added [Text.WriteTextFile](http://www.mbsplugins.eu/TextWriteTextFile.shtml) support for UTF-16 writing.

### Example Databases

- [CURL/Email/Email Parser and Preview](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/Email/Email%20Parser%20and%20Preview.shtml#2ScriptAnchor_)
- [CURL/Email/IMAP Email](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/CURL/Email/IMAP%20Email.shtml#6ScriptAnchor_)
- [Files/Write Text File](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Files/Write%20Text%20File.shtml#1GroupButton150.0000000138.0000000176.0000000271.0000000LayoutObjectAnchor_)
- [Shell/RunTask/Run Shell Script on Mac](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Shell/RunTask/Run%20Shell%20Script%20on%20Mac.shtml#1ScriptAnchor_)
- [Text functions/Text to RTF or HTML](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Text%20functions/Text%20to%20RTF%20or%20HTML.shtml#5ScriptAnchor_)
- [Text functions/Write Text File](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Text%20functions/Write%20Text%20File.shtml#2ScriptAnchor_)
- [Utility functions/Split DDR](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/Utility%20functions/Split%20DDR.shtml#1ScriptAnchor_)

### Blog Entries

- [Filemaker: Export Text to File](https://www.mbsplugins.de/archive/2025-06-26/Filemaker_Export_Text_to_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 12.2pr1](https://www.mbsplugins.de/archive/2022-04-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [New in MBS FileMaker Plugin 11.4](https://www.mbsplugins.de/archive/2021-09-15/New_in_MBS_FileMaker_Plugin_11/monkeybreadsoftware_blog_filemaker)
- [Define hot keys for functions in the FileMaker development](https://www.mbsplugins.de/archive/2019-11-15/Define_hot_keys_for_functions_/monkeybreadsoftware_blog_filemaker)
- [FileMaker 18 File Script Steps vs. BinaryFile functions](https://www.mbsplugins.de/archive/2019-06-03/FileMaker_18_File_Script_Steps/monkeybreadsoftware_blog_filemaker)
- [CSV functions in MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2017-11-01/CSV_functions_in_MBS_FileMaker/monkeybreadsoftware_blog_filemaker)
- [Comparing Base Elements Plugin to MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2017-10-24/Comparing_Base_Elements_Plugin/monkeybreadsoftware_blog_filemaker)
- [Filemaker: Export Text to File](https://www.mbsplugins.de/archive/2012-11-08/Filemaker_Export_Text_to_File/monkeybreadsoftware_blog_filemaker)
- [MonkeyBread Software Releases the MBS Filemaker plug-in in version 2.4](https://www.mbsplugins.de/archive/2011-10-11/MonkeyBread_Software_Releases_/monkeybreadsoftware_blog_filemaker)
- [MBS Filemaker Plugin 2.4 release notes](https://www.mbsplugins.de/archive/2011-10-11/MBS_Filemaker_Plugin_24_releas/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 2/2019, Seite 14](https://filemaker-magazin.de/neuigkeit/4043-Appetithappen-FMM_201902)
- [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 3th July 2024

  
[Text.UpperCase](TextUpperCase.md) - [Text.WriteToContainer](TextWriteToContainer.md)

[HTML Version](TextWriteTextFile.shtml)