I cannot pass the value+1 to write file from a loop

Hi everyone,

I’m trying to save multiple local files in n8n using a loop.
My workflow works fine overall — a Code node generates several items, each with an id or fileBase field (for example: clip_001, clip_002, etc.).

Then I use a Split In Batches node with Batch Size = 1 to process each item one by one, and a Write File node to save them under /files/.

The issue is that every time a file gets written, the previous one is overwritten — instead of producing separate files (like clip_001.txt, clip_002.txt, etc.), I only end up with one file.

I’ve tried different expressions for the file name, like:

/files/{{ $json.fileBase }}.txt  
/files/test_{{ $itemIndex + 1 }}.txt  
/files/test_{{ $json.id + 1 }}.txt

but none of them seem to create unique outputs — the result is always a single file being updated.

So my questions are:

  • How can I correctly pass a unique value (for example, item + 1 or Date.now()) to generate unique file names for each loop iteration?

  • And what’s the proper setup for the Write File node when saving simple text (not binary)?

Any guidance or example JSON would be greatly appreciated.

Thanks a lot!

If you use Loop and you want to achieve item + 1 style, you can use {{ $runIndex }} expression for that.

Thanks. So instead of /rep/clip_{{$itemIndex+1}}.png I should write /rep/clip_{{$runIndex}}.png ? But it would not be 1 2 3 4 maybe. I guess it would be a random number?

It will be 1 2 3 …, because if you are using the Loop node, the $runIndex will add +1 each loop is success.

Maybe it will look like this

Each Success, the $runIndex will be +1, and it’s started from 0

1 Like

@Super_Friend If it’s helps, kindly mark my message as the solution. This will help others with the same problem find the answer easily.

1 Like

Thanks a lot. It did work.

1 Like

You’re welcome bud

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