# Getting the file baseName depending on the file extension

**URL:** <https://community.seqera.io/t/getting-the-file-basename-depending-on-the-file-extension/622>\
**Category:** Tips & Tricks\
**Tags:** nextflow\
**Created:** [April 3, 2024, 7:29am UTC](https://community.seqera.io/t/getting-the-file-basename-depending-on-the-file-extension/622 "2024-04-03T07:29:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [April 3, 2024, 7:29am UTC](https://community.seqera.io/t/getting-the-file-basename-depending-on-the-file-extension/622/1 "2024-04-03T07:29:24Z")

</div>

It’s a common need in scripting is to want to retrieve the base name of a file, i.e. the name of the file without the extension. This is often used, for example, as a prefix to name other files you produce. This can be done using the file object `getBaseName()` function in Nextflow.

However, it’s also common that tools can read both plain text or compressed inputs by auto-detecting the file extension. However this can make scripting more cumbersome as `getBaseName()` only removes the last file extension.  
e.g.

```groovy
file("sampleA.ext").getBaseName() -> "sampleA"
file("sampleA.ext.gz").getBaseName() -> "sampleA.ext"

```

The `getBaseName()` function, however can also take an integer argument, which is the number of extensions to remove.

```groovy
file("sampleA.ext").getBaseName(1) -> "sampleA"
file("sampleA.ext.gz").getBaseName(2) -> "sampleA"

```

Using a test on the name, we can then make this test dynamic based on the input to automatically get the correct base name.

```groovy
myfile.getBaseName(myfile.name.endsWith('.gz')? 2: 1)

```

which uses a ternary operator to return 2 if the filename ends with ‘.gz’, otherwise it returns 1.

---

<div class="post-metadata">

**Author:** ![Alexander\_Nater](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/alexander_nater/32/552_2.png) [@Alexander\_Nater](https://community.seqera.io/u/Alexander_Nater)\
**Post date:** [April 3, 2024, 10:17am UTC](https://community.seqera.io/t/getting-the-file-basename-depending-on-the-file-extension/622/2 "2024-04-03T10:17:48Z")

</div>

There is also the very useful `getSimpleName()`, which removes all file extensions.

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [April 3, 2024, 11:20am UTC](https://community.seqera.io/t/getting-the-file-basename-depending-on-the-file-extension/622/3 "2024-04-03T11:20:00Z")

</div>

Good point. This really depends on how many `.` user’s use in their inputs.  
`getSimpleName()` strips everything after the first period.

```groovy
file("sampleA.deduped.normalised.fastq.gz").getSimpleName() -> "sampleA"
file("sampleA.deduped.normalised.fastq.gz").getBaseName(2) -> "sampleA.deduped.normalised"

```
