# How to print files names after collect in a process to a file

**URL:** <https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746>\
**Category:** Ask for help\
**Created:** [May 8, 2024, 7:34pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746 "2024-05-08T19:34:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 8, 2024, 7:34pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/1 "2024-05-08T19:34:27Z")

</div>

Hi there,  
I’m interested in printing all the files names generated after collect in a file.

```auto
process print_value {

publishDir path: "/data1/users/nextflow/learn_nextflow/files_created/", mode: 'copy' 
    input:
    val (temp_file)

    output:
    stdout

    script:
    """
    echo "$temp_file"  
    """
}

process file_create {

publishDir path: "/data1/users/nextflow/learn_nextflow/files_created/", mode: 'copy' 
    input:
    val (temp_number)

    output:
    path("${temp_number}.txt")

    script:
    """
    echo "value is $temp_number" > $temp_number".txt"
    """

}

workflow {

numbers=Channel.of(1, 2, 3, 40, 50)

ch_temp_path=file_create(numbers).collect()
}

```

I’d like to print the files created in the work directory to a file.  
For e.g.:

```auto
[/mnt/data1/users/nextflow/learn_nextflow/work/f9/085a49b37b3b75cd1cc5b031d68f8c/2.txt, /mnt/data1/users/nextflow/learn_nextflow/work/2d/0cfcebbee64cfc8563760ce62c7fe6/3.txt, /mnt/data1/users/nextflow/learn_nextflow/work/cd/236ea42f7246aea4b5b6f5e136f3e6/50.txt, /mnt/data1/users/nextflow/learn_nextflow/work/18/0a9d8f8131b5011a9c9a6fb0f84ef2/1.txt, /mnt/data1/users/nextflow/learn_nextflow/work/43/d5dfbf7e6e9b91af13afc831b98a8e/40.txt]

```

I want to print all these names in a list.txt

I’ll use this list.txt eventually using a bash script.

How do I code it?

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [May 9, 2024, 1:11am UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/2 "2024-05-09T01:11:15Z")

</div>

You can use the `collectFile` channel operator. It writes the content of a channel to an output file. Read more about it [here](https://www.nextflow.io/docs/latest/operator.html#collectfile).

---

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 9, 2024, 1:08pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/3 "2024-05-09T13:08:03Z")

</div>

@mribeirodantas  
Thanks.  
How do I do what here?  
Sorry, I didn’t understand how to use it after collect. or how to use it after passing the collected files.

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [May 9, 2024, 8:46pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/4 "2024-05-09T20:46:25Z")

</div>

Hey @complexgenome

There are examples with `collectFile` in the link I shared. Did you try running those examples? Did you try writing your version based on those examples? In what form is your adapted code version, and what error is it giving to you?

---

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 10, 2024, 2:15pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/5 "2024-05-10T14:15:15Z")

</div>

@mribeirodantas  
Yes, I’m unable to write code for it thus I asked how do I do what here.  
Nevertheless, I tried to write code, where I’m unable to understand how to proceed.

```auto

workflow {

numbers=Channel.of(1, 2, 3, 40, 50)

file_create(numbers).collect(flat: false).collectFile(name: 'sample.txt', newLine: true)
    .subscribe {
        println "\n Entries are saved to file: $it \n"
        //println "File content is: ${it.text}"
    }

}

```

I tried to find the “sample.txt” couldn’t find it in the work directory. Or, how do I access it in the nextflow?

Second the print overwrite the output. Please see attached screenshot.

 ![Screenshot 2024-05-10 at 10.13.46 AM](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/93759df7a175fd7f1deb540feb42f23d63305d45.png)

---

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 13, 2024, 2:25pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/6 "2024-05-13T14:25:57Z")

</div>

@mribeirodantas  
Can you please help?

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [May 13, 2024, 3:31pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/7 "2024-05-13T15:31:55Z")

</div>

Hi @complexgenome. I’m sorry you’re having a difficult time writing Nextflow code. Unfortunately, there’s only so much time our team can dedicate to individual users. It’s important for the good of the community that we prioritize work that helps everyone, like developing new training materials and documentation. Because of that, I’m not going to be able to answer all of your questions in as much detail as you want. It sounds like maybe you need to spend a bit more time going through the educational resources, or maybe find a collaborator in your local network who could help you.

You can find a lot of resources that will help you at [https://docs.seqera.io](https://docs.seqera.io) (check the bottom of the page).

---

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 13, 2024, 4:24pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/8 "2024-05-13T16:24:39Z")

</div>

@mribeirodantas  
I understand. I’m in the last leg of the pipeline therefore seeking help.

I tried what I could and shared the code. Unfortunately, it is counter intuitive to me on how to proceed further.

I’m sorry that I find Nextflow complicated for me and to work through. Since the pipeline is an in-house thing it’s difficult to craft through the documentation, examples as the case is usually unique.  
I’ve come a long way starting from scratch in nextflow until here.

Thank you for your help and replies, as always.

---

<div class="post-metadata">

**Author:** ![bentsherman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bentsherman/32/52_2.png) [@bentsherman](https://community.seqera.io/u/bentsherman)\
**Post date:** [May 15, 2024, 2:54pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/9 "2024-05-15T14:54:07Z")

</div>

collectFile allows you to concatenate a list of text files into a single text file. But if you want to save the file paths to a file, like an index file, you’ll need to do that manually.

I suggest you pass the list of paths into an exec process and write the paths to an output file. Here are some relevant docs to help you along:

- [Scripts — Nextflow documentation](https://nextflow.io/docs/latest/script.html#files-and-i-o)
- [Processes — Nextflow documentation](https://nextflow.io/docs/latest/process.html#native-execution)

---

<div class="post-metadata">

**Author:** ![complexgenome](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/complexgenome/32/392_2.png) [@complexgenome](https://community.seqera.io/u/complexgenome)\
**Post date:** [May 20, 2024, 3:46pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/11 "2024-05-20T15:46:01Z")

</div>

@bentsherman  
Thank you for your attention to my post.

I’m sorry as I’m not intelligent for nextflow’s advanced system. I’m to create pipeline in nextflow thus I use this platform to troubleshoot.  
The tutorials/links are fine for a small example, however, they do not help with unique or some specific pipeline example.

For example:

```auto
process file_create {

publishDir path: "/data1/users/nextflow/learn_nextflow/files_created/", mode: 'copy' 
    input:
    val (temp_number)

    output:
    tuple val (temp_number),path("${temp_number}.txt"), emit: temp_file

    script:
    """
    echo "value is $temp_number" > $temp_number".txt"
    """

}

workflow {

numbers=Channel.of(1, 2, 3, 40, 50)
file_create(numbers).collectFile(name: 'sample.txt', newLine: true)

}

```

In this case the sample.txt isn’t generated.

However, if I remove “val(temp\_number)” from the output of `file_create` process this sample.txt file is generated.

Sadly, I’m stuck at this particular step for over a week and none of the examples cover such situations, or, so to say I’m not able to build on top of the existing example.

Further, is there a way to pass this “sample.txt” around different processes?

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [May 20, 2024, 8:34pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/12 "2024-05-20T20:34:48Z")

</div>

Hey @complexgenome.

You need to extract the path from the tuple in your output channel before passing it to `collectFile`. See the snippet below:

```auto
process FOO {
  input:
  val x

  output:
  tuple val("${x}"), path("${x}.txt")

  script:
  """
  echo "Oi" > ${x}.txt
  """
}

workflow {
  Channel
    .of(1..5)
    | FOO
  FOO
    .out
    .map { it[1].name }
    .collectFile(name: "sample.txt", newLine: true)
}

```

As the operation occurs outside a process, you won’t find the file in a task directory, but in the temporary directory. Example below:

 ![CleanShot 2024-05-20 at 16.31.33@2x](https://europe1.discourse-cdn.com/flex013/uploads/seqera/original/1X/60fe9f9d4b2736596d409f743464bb1db79cdbe9.png)

If you want another process to work on this file, you don’t need to know where it is located. Simply `set` to a channel and provide this channel as input to the other process. If you want to save it to a diffeent location, you can se this with the `collectFile` option `storeDir`.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [May 27, 2024, 8:35pm UTC](https://community.seqera.io/t/how-to-print-files-names-after-collect-in-a-process-to-a-file/746/13 "2024-05-27T20:35:08Z")

</div>

Closed on May 27, 2024, 8:35pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
