# File node: how to create an uniquely named file every time the node gets data passed through it?

**URL:** <https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177>\
**Category:** General\
**Created:** [24 February 2020 09:53 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177 "2020-02-24T09:53:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pseudonoob](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pseudonoob/32/6819_2.png) [@pseudonoob](https://discourse.nodered.org/u/pseudonoob)\
**Post date:** [24 February 2020 09:53 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/1 "2020-02-24T09:53:27Z")

</div>

My basic flow is getting an image URL, using the image-tools and file-in node to save that image to a file on my local server. I can then forward the new local URL, for home automation purposes.

I'm trying to get the file-in node to write every single request as a new file. Preferably logically, like `file1.jpg`, `file2.jpg`, etcetera. Currently I have it writing the file to `/local/file.jpg`, but I want to have every new image coming in to be written to an unique file.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c0fdd0bad7afa268636b75977ef8dd815bde483.png)

What's the best way to achieve this?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 February 2020 10:10 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/2 "2020-02-24T10:10:47Z")

</div>

put a `change` node before the `file` node to add a time stamp to the filename.

---

<div class="post-metadata">

**Author:** ![pseudonoob](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pseudonoob/32/6819_2.png) [@pseudonoob](https://discourse.nodered.org/u/pseudonoob)\
**Post date:** [24 February 2020 10:24 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/3 "2020-02-24T10:24:59Z")

</div>

I prefer to have it be file1, file2, file3, etc. What's the best way to achieve that?

Additionally do you have an example on how to change the filename to a variable? Is it msg.filename? What would the code look like with the jpeg part at the end?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 February 2020 10:29 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/4 "2020-02-24T10:29:34Z")

</div>

Then you'll need to keep track on counting.

```auto
var c = context.get('c') || 0
c ++
context.set('c',c)

var path = "path/to/your/desired/folder/image_"+c+".jpg"
msg.filename = path
return msg;

```

And you'll need to use persistent context storage option, to keep counting survive the restart of Node-RED

And even then it is not the most reliable way to do it.

---

<div class="post-metadata">

**Author:** ![pseudonoob](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pseudonoob/32/6819_2.png) [@pseudonoob](https://discourse.nodered.org/u/pseudonoob)\
**Post date:** [24 February 2020 17:16 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/5 "2020-02-24T17:16:54Z")

</div>

Thank you for this! I have an mqtt broker running that cpuld help with surviving node red restarts. Could that be used of persistent context storage? If not, how do I get it to stay persistent?

And if this isn't the most reliable way, how would you do it instead?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 February 2020 17:27 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/6 "2020-02-24T17:27:54Z")

</div>

Most reliable will be to examine previously saved files , parse names to be sure they are consecutively saved and figure out name for new file. Also folder should be changed if certain amount of files already saved to one folder to preserve manteanability and keep performance of file system steady.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 February 2020 17:28 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/7 "2020-02-24T17:28:52Z")

</div>

If you use a Retained topic in MQTT then that will remember it for you. Alternatively you could use Persistent Context in node-red.

> [@A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115):
>
> This is something I wrote up after playing with the context options. I've filed a PR with some of this added to the Node-RED documentation but thought I'd release it here also. I hope people find it useful. Paul A guide to understanding 'Persistent Context' Prior to Node-RED v0.19.0, data could be stored in context as a global, flow or node. This was stored in memory and would be reset with a restart of Node-RED. As of v.0.19.0 you can now store node, flow and global context data in memory O…

**[Edit]** But I like @hotNipi's idea better.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [24 February 2020 18:57 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/8 "2020-02-24T18:57:14Z")

</div>

> [@hotNipi](#):
>
> Most reliable will be to examine previously saved files , parse names to be sure they are consecutively saved and figure out name for new file. Also folder should be changed if certain amount of files already saved to one folder to preserve manteanability and keep performance of file system steady.

It is a good approach in my opinion. It could be easily implemented with the help of node " fs-ops-dir" from "node-red-contrib-fs-ops". This node will return in `msg.files` (you chose the name of the property) an array with the name of the files inside a given folder. You can then use a function node to get what is the index to be used for the next file name.

```auto
msg.payload = msg.files.length;
return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 April 2020 18:57 UTC](https://discourse.nodered.org/t/file-node-how-to-create-an-uniquely-named-file-every-time-the-node-gets-data-passed-through-it/22177/9 "2020-04-24T18:57:15Z")

</div>

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