Skip to content

Write File

Type: write-file  ·  Package: Filesystem Activities v1.0.6  ·  Output: string

Writes a text string to a file. If the file does not exist, it creates it; if it exists, it replaces it by default. Use it to produce output files: a log, a text report, a settings file.

Writes content to the file at path.

  • Overwriting: by default it replaces the existing file (overwrite = true). If you set overwrite = false and the file already exists, the activity fails instead of replacing it, leaving it untouched.
  • Folders: if the file’s folder does not exist, it is created (with any intermediate ones).
  • Encoding: with utf-8 (default) the file starts with the UTF-8 BOM, which Excel and Notepad recognize. See encoding.
  • Exact content: writes the text as is, without adding a line break at the end. If content is null, it creates an empty file.
  • Idempotent with overwrite = true: repeating it gives the same result.
ParameterTypeDefaultDescription
pathstring—Path of the file to write, including its folder.
contentstring—Text to write. Other types are converted to text.
ParameterTypeDefaultDescription
encodingstring"utf-8"File encoding: utf-8, iso-8859-1, utf-16, utf-32, us-ascii… See encoding.
overwritebooltrueReplace the file if it already exists. With false, the activity fails if it exists.

Returns a string with the path of the written file (as you gave it in path).

MessageCause and fix
The file already exists and 'overwrite' is false.You set overwrite = false and the file exists. Use a unique name, delete it first, or keep overwrite = true.
The process cannot access the file '…' because it is being used by another process.The file is open in another program.
Access to the path '…' is denied.No write permission on the folder, or the file is read-only.
'windows-1252' is not a supported encoding name…The encoding is not available. Use iso-8859-1 or utf-8.
'path' is required.The path expression evaluated to null.

If path is just a file name, it is written to the robot’s working folder (since version 1.0.6; before, the activity failed). Use a full path so you always know where the file ends up.

Produce a summary and attach it to an email:

Write File path = pathJoin(outputFolder, "summary.txt") content = "Process completed: " + now().ToString("yyyy-MM-dd HH:mm") → output: summaryPath
SMTP Send ... subject = "Process summary" attachments = arr(summaryPath)