Append characters to the contents of an existing file indicated by the SetDefaultFileSpec function.
TrFile_AppendContents ( switches ; text )
|switches||these alter the behavior of the function|
|text||the characters that you want to add at the end of the default file|
Switches can be empty or one of this:
|-Encoding=BytesOnly||only characters in the range 0-255 are written, others are skipped|
This determines the character encoding of the appended data.
You can also add this switch:
|-CRtoCRLF||Use Windows line endings. Returns in the text parameter are substituted with CRLF (Carriage Return followed by a Line Feed) in the written file.|
Data type returned
The returned result is zero or an error code. An error always starts with 2 dollars, followed by the error code. You should always check for errors. Returned error codes can be:
|0||no error||The string of characters was added to the file|
|$$-1||genericErr||The file could not be opened for appending (You might need to create the file first)|
|$$-2||genericErr||The file could not be opened for appending (You might need to create the file first)|
Other errors may be returned.
Troi File Plug-in 1.1
FileMaker Pro 16 to 19
Use the function TrFile_SetDefaultFileSpec first to set the default file. See also the functions:
TrFile_SaveFileDialog to ask the user for a FileSpec, TrFile_SetDefaultFileSpec to indicate the file to affect.
Note that the maximum size of TrFile_AppendContents is currently 150 million characters (per call to the function). This equals to about max. 600 million characters written. This depends on the encoding, for example one Unicode character encoded as UTF-8 can require up to 4 characters in the written file.
Up to v5.0 there was an error in the documentation: it was incorrectly stated that UTF-8 was the default encoding. The default is -Encoding=Native, which is the current encoding as set in the system preference, for example on western Windows systems it usually is ISO_8859_1 or ASCII_Windows. On macOS the default will be ASCII_Mac.
To be able to export all possible characters (like chinese characters) it is best to export with encoding UTF8.
Assume your C disk already contains a file “Testtext.txt”, which contains the text “Hello”. We assume that a global number field gErrorCode is defined. Create the following script:
Set Field [ gErrorCode ; TrFile_SetDefaultFileSpec ( "-Unused" ; "C:\Testtext.txt" ) ] If [ gErrorCode = 0 ] Set Field [ gErrorCode ; TrFile_AppendContents ( "-Unused" ; " there." ) ] End If
This script will add the text ” there.” to the end of the file. After running the script once the file will contain “Hello there.”. If you run the script again the file will contain “Hello there. there.”
This shows how to write to a log file. We assume that in your FileMaker file the following fields are defined:
gErrorCode Global, number gXplatformReturn Global, text gLogFilePath Global, text gLogtext Global, text
gLogFilePath should contain the path to an existing log file, for example “D:\Logs\L2017_01.TXT” (Windows) or “Mac HD:Logs:Log 2000_01” (Mac). gXplatformReturn should contain a return on Mac and a return and a linefeed on Windows. gLogtext is the text you want to log. Add the following scriptstep to your startup script:
Set Field [ gErrorCode ; TrFile_SetDefaultFileSpec ( "-Unused" ; gLogFilePath ) ]
This will set the default file to your log file. Then whenever you need to write a line to the log fill the field gLogtext and perform this script step:
Set Field [ gErrorCode ; TrFile_AppendContents ( "-Encoding=UTF8" ; DateToText ( Get ( CurrentDate ) ) & " " & TimeToText ( Get ( CurrentTime ) ) & " text: " & gLogtext & gXplatformReturn ) ) ]
This script step will add a line every time this script is performed. The log file might look like this:
12/31/2016 10:23:56 text: user Peter logged in
12/31/2016 14:31:02 text: user Peter logged out
12/31/2016 14:31:02 text: closed file “Invoices.fmp12”
and so on…..
Used in example file