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.
AppendTextToFileappends the text at the end of the file (if it exists).WriteTextToFileoverwrites 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.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
filename | string | required | Path to the file you wish to write to |
text | string | required | The text string to write to the file |
| Return value | Explanation |
|---|---|
true | The text was written to the file |
false | The 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.');
}