Skip to content

Write text to file (2 ways)

SyncJS provides two functions to write text to a file:

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

Both functions write text to a file and return true if the operation is successful, or false otherwise. If the file does not exist, it will be created automatically.

  • AppendTextToFile appends the text at the end of the file (if it exists).
  • WriteTextToFile overwrites the file with the specified text, erasing any existing content.

Both functions support escaped strings. For example, to write a sentence followed by a newline, use \n in the text. String escaping follows this convention.

ParameterTypeRequirementExplanation
filenamestringrequiredPath to the file you wish to write to
textstringrequiredThe text string to write to the file
Return valueExplanation
trueThe text was written to the file
falseThe operation failed; the file was not written or modified

Example: Append text

ts
var result = AppendTextToFile('/home/user/docs/somefile.txt', 'Hello world!\n');
if (result) {
  Log('Text appended successfully.');
} else {
  Log('Append failed.');
}

Example: Overwrite text

ts
var result = WriteTextToFile('/home/user/docs/somefile.txt', 'Hello world!\n');
if (result) {
  Log('Text written successfully.');
} else {
  Log('Write failed.');
}