# FileParser task

A FileParser reads a text file and turns what is in it into values a job can use. A CSV file of orders becomes a list with one item per line and a variable per column; a small settings file becomes variables; a whole file becomes one variable.

It needs an Enterprise licence.

## Three ways to read

**Read entire file as**:

| Choice | Gives | For |
|---|---|---|
| **None** | one new item **per line**, with a variable per column | CSV and other tables |
| **{file_contents}, no parsing.** | the file's text as `{file_contents}` of the file's item | a small file whose text goes into a mail, an HTTP call, or a [function](Jobs-Functions.md) that picks it apart |
| **Properties** | every line `name=value` of the file as a variable `{name}` of the file's item, and the text as `{file_contents}` | control files that travel with a data file |

## Fields

| Field | What it does | On a new task |
|---|---|---|
| **First line of file contains column names.** | the columns get the names of the first line, and that line is not an item | off |
| **Column Separator** | **Comma**, **Semicolon**, **Tab**, **Colon**, or **Fixed** for columns of fixed width | Comma |
| **Text Encoding** | how the file's bytes are read as text: `UTF8`, `ISO-8859-1`, `windows-1252` | `UTF8` |
| **Max lines to parse** | the most lines to read. Empty or 0: up to a million. A **negative** number reads that many lines from the **end** of the file, last line first. | 0 |
| the columns: **Index**, **Column Name**, **Size** | for files without a header line: the first column is index 1, and gets this name. **Size** is the width, for **Fixed**. **Add more..** for each further column. | |

"Provide atleast one column information to parse file properly."

Values in double quotes may hold the separator: `"Smith, Anna",42` is two columns.

## What a line becomes

With a file

```
order;customer;amount
1001;ACME;250.00
1002;Globex;99.50
```

a header line and the separator Semicolon, the task adds two items:

| Variable | First item | Second item |
|---|---|---|
| `{order}` | `1001` | `1002` |
| `{customer}` | `ACME` | `Globex` |
| `{amount}` | `250.00` | `99.50` |
| `{row}` | `1` | `2` |
| `{line}` | the whole line | |

Without names, the columns are `{0}`, `{1}`, `{2}`.

The file's own item stays on the list too, in front, and gets `{result_count}`: how many lines were read.

## What it hands on

The items it was given, and behind them the new items of the lines. The lines are not files: their address begins with `text://`. A task that copies or deletes files should not be given them. Either give the following tasks a filter, or put an [Exclude](Task-Exclude.md) for the file before them.

## Uses

**Do something per line.** A [Loop](Task-Loop.md) for each item behind the parser, with an HTTP call or an SQL statement that uses `{customer}` and `{amount}`.

**A file that says where the data goes.** A partner sends `data.zip` and `data.ctl` with `target=/in/orders` in it. Parse the `.ctl` as **Properties**, and the Copy uses `{target}`.

**A mail with the file's text.** Read a small file as `{file_contents}` and put that into an [Email](Task-Email.md)'s body.

**The last lines of a log.** **Max lines to parse** `-20` gives the last twenty lines, newest first.

## Things to know

- **Every line is an item in memory.** That is why a run reads no more than a million lines unless you say so. For big files, read what you need: the first lines with a positive number, the last with a negative one.
- **Text only.** A spreadsheet file (`.xlsx`) is not text and cannot be parsed. Have it sent as CSV.
- **The right encoding.** Umlauts that come out as two odd characters mean the file is not UTF-8: try `windows-1252`.
- **What a column holds is data.** A cell with braces in it, such as `{name}`, is not read as a variable by the tasks that follow.
- **The file must be where the item says.** The task reads the item's own file and refuses a name that would lead to another folder: "File Parser: refusing to read …".
- **Writing** such files is the [WriteFile](Task-WriteFile.md) task.
