# basename

Learn how to extract the filename from a path.

Returns the filename portion of a path, optionally without the extension.

## Example

The following template extracts and prints the filename from a path, with and without the extension:

**template.json5**

```json5
{
  "$schema": "https://schema.croct.com/json/v1/template.json",
  "title": "Basename demo",
  "description": "Prints the filename from a path.",
  "options": {
    "file": {
      "type": "string",
      "description": "The file path.",
      "default": "src/components/Button.tsx"
    }
  },
  "actions": [
    {
      "name": "print",
      "message": "Full: ${basename(options.file)}, without extension: ${basename(options.file, true)}"
    }
  ]
}
```

With the default value, this prints `Full: Button.tsx, without extension: Button`.

## Parameters

- `path`: `string`

  The file path to extract the filename from.

- `omitExtension`: `boolean` (optional) (default: false)

  Whether to omit the file extension from the result.

## Return

The filename portion of the path.

For example, `basename('src/components/Button.tsx')` returns `Button.tsx`, and `basename('src/components/Button.tsx', true)` returns `Button`.

## Explore

- [Dirname](/reference/cli/templates/functions/dirname): Learn how to extract the directory from a path.
- [Ext](/reference/cli/templates/functions/ext): Learn how to extract the file extension.
