Skip to content

Write text to file (2 ways)

SyncJS actually does offer 2 distinct functions to write text to file:

ts
function AppendTextToFile(filename, text: string): boolean;
function WriteTextToFile(filename, text: string): boolean;

Both of these functions write text to a file and return true if the operation was successful, otherwise they return false. Also, both functions automatically create the file if it doesn’t exist.

The main difference is that the AppendTextToFile function will append text at the end of a file (if it exists) whereas the WriteTextToFile will overwrite whatever contents are already in a file with the specified text, and all pre-existing file content will be lost.

Both functions support escaped strings, so, for example, if you want to write a sentence and then a NEWLINE special control character, you can simply add \n to the text to be written to file. String escaping follows this convention.

This function accepts the following parameters:

ParameterTypeRequirementExplanation
filenamestringrequiredmust be a valid and existing path to a file you wish to write into
textstringrequiredthe text string you want to write to the file

Possible return values:

ValueExplanation
truethe function succeeded: the text was written into the file
truethe function failed: the file was not written/modified

Example

ts
{
  AppendTextFile('/home/someuser/docs/somefile.txt', 'Hello world!\n');
}