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

Text.WriteTextFile

Writes a text file with the given text.

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

(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.
"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 to define which line endings the file has.
In order to append to a text file, you can use Text.AppendTextFile 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 for reading text files.
If you need to create directory first, please use Files.CreateDirectory function.

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

Write UTF-8 file with some text:

MBS("Text.WriteTextFile"; $Text; $Path; "utf8" )

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) & "Hello World"; $Path; "utf8")

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"; "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"; $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) & "Hello World"; "/Users/cs/Desktop/test UTF-16LE.txt"; "UTF-16LE")

MBS("Text.WriteTextFile"; Char(65279) & "Hello World"; "/Users/cs/Desktop/test UTF-16BE.txt"; "UTF-16BE")

MBS("Text.WriteTextFile"; Char(65279) & "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"; $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" ) ]
Set Variable [ $path ; Value: MBS( "Path.AddPathComponent"; $folder; "test.jpg" ) ]
Set Variable [ $r ; Value: MBS( "Container.WriteFile"; Images::Image; $path ) ]
#
Set Variable [ $html ; Value: "<html><body><p>Hello World</p><img src=test.jpg width=300></body></html>" ]
Set Variable [ $path ; Value: MBS( "Path.AddPathComponent"; $folder; "test.html" ) ]
Set Variable [ $r ; Value: MBS( "Text.WriteTextFile"; $html; $path ) ]
#
Set Variable [ $URL ; Value: MBS( "Path.FilePathToFileURL"; $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"; $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

Release notes

Example Databases

Blog Entries

This function is free to use.

Created 18th August 2014, last changed 26th October 2023


Text.UpperCase - Text.WriteToContainer