# WriteFile task

A WriteFile writes text into a file: a report of what a job handled, a line per file in a log of your own, a small control file that tells another program "the batch is complete", an XML or CSV that is built up run by run.

## Fields

| Field | What it does | On a new task |
|---|---|---|
| **File Path** | the file to write, as an address: `file:///data/out/arrivals-{yyyy}{MM}.txt`. **Browse**. | |
| **Overwrite?** | the file is written afresh each time, from **New File**. Unticked, a file that is there is kept and added to. | off |
| **Loop through items?** | the task writes once **per item**, so that `{name}` and `{size}` are each file's own. Unticked, it writes once for the whole list. | off |
| **Append Only** | with the loop: each item's text is added at the end of the file, without reading the file first. The fast way to keep a log. | off |
| **Add to the list for future tasks** | the written file is put on the list, so that a later task can send or copy it | off |
| **Route connection through DMZ when possible** | the connection is made from a DMZ server | |

The text is given in up to four parts. Each is used when it is filled in:

| Part | Fields | What it does |
|---|---|---|
| **New File** | **Contents** | written when the file does not exist yet, or is empty, or **Overwrite?** is ticked |
| **Add Before** | **Find**, **Contents** | looks for the **Find** text from the beginning of the file and puts the contents in front of it |
| **Add After** | **Find**, **Contents** | looks for the **Find** text from the end of the file and puts the contents behind it |
| **Find / Replace** | **Find Text**, **Replace With** | replaces a text in the file |

All parts take [variables](Jobs-Variables.md) and [functions](Jobs-Functions.md). `{r}{n}` ends a line. The file is written as UTF-8.

## Three ways to use it

**A line per file, in a log.**

| Field | Value |
|---|---|
| **File Path** | `file:///data/logs/arrivals-{yyyy}{MM}.txt` |
| **Loop through items?**, **Append Only** | ticked |
| **Contents** | `{yyyy}-{MM}-{dd} {HH}:{mm};{name};{size}{r}{n}` |

**A report of this run**, written once and sent.

| Field | Value |
|---|---|
| **File Path** | `file:///data/out/report-{yyyy}{MM}{dd}.txt` |
| **Overwrite?**, **Add to the list for future tasks** | ticked |
| **Contents** | `Files delivered on {dd}.{MM}.{yyyy}: {items_count}{r}{n}{line_start}{name} {size}{r}{n}{line_end}` |

Without the loop, the part between `{line_start}` and `{line_end}` is repeated for every item, and what is outside is written once.

**A file that grows inside**, such as a feed or an XML list: **New File** writes the frame once, with a marker in it such as `<!--next-->`, and **Add Before** with that marker as **Find** puts each new entry in front of the marker. The file stays well-formed after every run.

## What it hands on

The items it was given, and with **Add to the list for future tasks** the written file behind them.

## Things to know

- **With an empty list** the task still writes once: a control file at the end of a job does not need items.
- **Not writing.** Contents that begin with `!!!NO_WRITE` skip the write. With a [function](Jobs-Functions.md) that puts that text there under some condition, a task can decide by itself.
- **Two runs at once.** Writes to the same file from two runs of the server are done one after the other, not into each other.
- **Large files.** Except with **Append Only**, the task reads the whole file, changes it and writes it back. Keep files that are added to in the middle small; start a new one per month.
- **The folder must exist**, as for a Copy.
- **Reading a file** into variables is the [FileParser](Task-FileParser.md) task.
