# write-file

Learn how to write content to a file.

This action writes content to a file, creating it if it does not exist or replacing it if it does. The content can include template expressions to generate dynamic values at runtime.

## Example

The following template creates a configuration file with a project name provided as an option:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "Config creator",
  "description": "Creates a configuration file.",
  "options": {
    "name": {
      "type": "string",
      "description": "The project name.",
      "default": "my-app"
    }
  },
  "actions": [
    {
      "name": "write-file",
      "path": "config.json",
      "content": "{\n  \"name\": \"${options.name}\"\n}\n"
    }
  ]
}
```

To apply this template, run:

```sh
croct use template.json5 --name my-app
```

## Properties

These are the supported properties:

- `name`: `string`

  The action identifier. Always `write-file` for this action.

- `path`: `string`

  The path to the file to write.

- `content`: `string`

  The content to write to the file.

- `overwrite`: `boolean` (optional)

  Whether to overwrite the file if it already exists.

  When not set, a confirmation prompt appears asking the user whether to replace the existing file.

## Explore

- [Read file](/reference/cli/templates/actions/read-file): Learn how to read existing file contents.
- [Replace file content](/reference/cli/templates/actions/replace-file-content): Learn how to replace content in an existing file.
- [Format code](/reference/cli/templates/actions/format-code): Learn how to format the written file.
- [Create directory](/reference/cli/templates/actions/create-directory): Learn how to ensure the target directory exists.
