Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.1   6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3   7.4    Statistic  

Text.WriteTextFile

Writes a text file with the given text.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
Text 2.4 Yes Yes Yes Yes Yes

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

(old name: String.WriteTextFile)

Parameters

Parameter Description Example value
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 Optional
The text encoding for text parameter.
Default is native.
Possible encoding names: ANSI, Arabic-Mac, Arabic-Win, Baltic-Win, CentralEurope-Mac, ChineseSimp-Mac, ChineseSimp-Win, ChineseTrad-Mac, ChineseTrad-Win, Cyrillic-Mac, Cyrillic-Win, EasternEurope-Win, Greek-Mac, Greek-Win, Hebrew-Mac, Hebrew-Win, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-15, Korean-Johab, Korean-Mac, Korean-Win, Latin1, Mac, Native, ShiftJIS-Mac, ShiftJIS-Win, Turkish-Mac, Turkish-Win, UTF-8, DOS or Windows. Pass native to use the native encoding of the current platform.
"UTF-8"

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.
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.

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

See also

Example Databases


Text.UpperCase   -   Text.WriteToContainer

Feedback: Report problem or ask question.




Links
MBS Xojo blog