Topics
All
MacOS
(Only)
Windows
(Only)
Linux
(Only, Not)
iOS
(Only, Not)
Components
Crossplatform Mac & Win
Server
Client
Old
Deprecated
Guides
Examples
Videos
New in version:
12.1
12.2
12.3
12.4
12.5
13.0
13.1
13.2
13.3
13.4
Statistic
FMM
Blog
Writes a text file with the given text.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
Text | 2.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
(old name: String.WriteTextFile)
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 |
Returns "OK" or an error message.
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")]
This function is free to use.
Created 18th August 2014, last changed 4th August 2023