# Msg.filename logging data

**URL:** https://discourse.nodered.org/t/msg-filename-logging-data/1117
**Category:** General
**Created:** [27 June 2018 09:34 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117 "2018-06-27T09:34:40Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 09:34 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/1 "2018-06-27T09:34:40Z")

</div>

Hi There,

I am looking to log some data to a local drive that will have the filename set as the date, thus each new day a new file with data will be added into the directory. I have read that you alter the ‘msg.filename’ in a function node prior to sending it to a file node, which is then left blank, correct?

I am new to javascript and I am struggling setting the ‘filename’ I have got some thing like this in my function node:

msg.filename = msg.filename.C:\Users\admin\node\_modules\zData\_Logged\Wave Buoy\Date().toString().txt;

I am on a Windows 10 OS.

I hope this is clear.

Thanks in advance

---

<div class="post-metadata">

### Author: ![ThanosMel](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@ThanosMel](https://discourse.nodered.org/u/ThanosMel)
#### Post date: [27 June 2018 09:42 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/2 "2018-06-27T09:42:43Z")

</div>

I am new to node red and javascript too,but try this msg.filename = “C:\Users\admin\node\_modules\zData\_Logged\Wave Buoy\Date().toString().txt”;

---

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 09:57 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/3 "2018-06-27T09:57:14Z")

</div>

Hi Thanos,

I believe this may work, however when I use the directory in the function node it says I cannot write to the directory - in the format you have stated above - but when I put in into the file node in the title - the directory - it will write to my local drive…

Do you know what may be causing this?

Bowen

---

<div class="post-metadata">

### Author: ![ThanosMel](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@ThanosMel](https://discourse.nodered.org/u/ThanosMel)
#### Post date: [27 June 2018 10:06 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/4 "2018-06-27T10:06:16Z")

</div>

I believe .toString() method is not working cause it is inside brackets. Try to  
var tempString = Date().toString();  
var tempString1 = “C:\Users\admin\node\_modules\zData\_Logged\Wave Buoy";  
var tempString2 = tempString.concat(tempString1);  
var finalPath = tempString2.concat(".txt");  
msg.filename=finalPath;  
I am not sure if that helps i dont have a clear view of the whole problem. Its maybe because of the \ try to use / inside your function. You can always use debug node and see the output of the function,

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [27 June 2018 10:33 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/5 "2018-06-27T10:33:19Z")

</div>

To use Windows pathnames in a string you need to escape the `\` character. So for example,

```auto
var tempString1 = “C:\Users\admin\node_modules\zData_Logged\Wave Buoy";

```

should be

```auto
var tempString1 = “C:\\Users\\admin\\node_modules\\zData_Logged\\Wave Buoy";

```

Or, use `/` instead:

```auto
var tempString1 = “C:/Users/admin/node_modules/zData_Logged/Wave Buoy";

```

Two other comments:

1. I wouldn’t be putting application data under a `node_modules` directory
2. when sharing code or flows on this forum, wrap it in three backtick characters (```) to ensure it is formatted properly.

---

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 10:43 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/6 "2018-06-27T10:43:18Z")

</div>

Hi @knolleary,

I have took your advice and changed the directory location. Is there meant to be different `"` in your solutions?

I had flipped the `/` however, if my flow is as follows it will write to my local directory:

`[{"id":"306a79fa.1dc546","type":"file","z":"e4b7c42f.224998","name":"","filename":"C:\\Users\\admin\\Documents\\Data Logged\\WaveBuoy\\{{Date().toString()}}.txt","appendNewline":true,"createDir":true,"overwriteFile":"false","x":990,"y":1300,"wires":[]},{"id":"985033d2.b4e7a","type":"csv","z":"e4b7c42f.224998","name":"","sep":",","hdrin":true,"hdrout":"","multi":"one","ret":"\\r\\n","temp":"","skip":"0","x":570,"y":1300,"wires":[["306a79fa.1dc546"]]},{"id":"fdecdcfa.c76f4","type":"function","z":"e4b7c42f.224998","name":"test","func":"var tempString = Date().toString();\nvar tempString1 = \"C:\\\\Users\\\\admin\\\\Documents\\\\Data Logged\\\\WaveBuoy\\\\\";\nvar tempString2 = tempString1.concat(tempString);\nvar finalPath = tempString2.concat(\".txt\");\nmsg.filename=finalPath;\nreturn msg;\n","outputs":1,"noerr":0,"x":390,"y":1300,"wires":[["985033d2.b4e7a"]]},{"id":"178df77e.df8839","type":"tcp in","z":"e4b7c42f.224998","name":"","server":"client","host":"xxx","port":"xxx","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":150,"y":1300,"wires":[["fdecdcfa.c76f4"]]}]`

But if my flow is this it doesn’t find the directory… Any ideas why this may be the case?

`[{"id":"178df77e.df8839","type":"tcp in","z":"e4b7c42f.224998","name":"","server":"client","host":"xxx","port":"xxx","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":150,"y":1300,"wires":[["fdecdcfa.c76f4"]]},{"id":"fdecdcfa.c76f4","type":"function","z":"e4b7c42f.224998","name":"test","func":"var tempString = Date().toString();\nvar tempString1 = \"C:\\\\Users\\\\admin\\\\Documents\\\\Data Logged\\\\WaveBuoy\\\\\";\nvar tempString2 = tempString1.concat(tempString);\nvar finalPath = tempString2.concat(\".txt\");\nmsg.filename=finalPath;\nreturn msg;\n","outputs":1,"noerr":0,"x":390,"y":1300,"wires":[["985033d2.b4e7a"]]},{"id":"985033d2.b4e7a","type":"csv","z":"e4b7c42f.224998","name":"","sep":",","hdrin":true,"hdrout":"","multi":"one","ret":"\\r\\n","temp":"","skip":"0","x":570,"y":1300,"wires":[["3d3edafb.4fa386"]]},{"id":"3d3edafb.4fa386","type":"file","z":"e4b7c42f.224998","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","x":890,"y":720,"wires":[]}]`

---

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 10:47 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/7 "2018-06-27T10:47:16Z")

</div>

Thanks Thanos.

All good so far, apart from:

`var tempString2 = tempString1.concat(tempString);`

Otherwise thank you very much. Quickly, is there an easy way to set the date to the following format `ddmmyyyy` or `yyyymmdd`?

Also do you know what may be the reason that the directory cannot be identified when using your `msg.filename = finalPath;` but does when putting the directory path in the file node?

---

<div class="post-metadata">

### Author: ![ThanosMel](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@ThanosMel](https://discourse.nodered.org/u/ThanosMel)
#### Post date: [27 June 2018 11:03 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/8 "2018-06-27T11:03:01Z")

</div>

try this exactly inside your node function :

```
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
var tempString =formatDate(Date().toString());

var tempString1 = "C:\\Users\\admin\\node_modules\\zData_Logged\\Wave Buoy\\";
var tempString2 = tempString1.concat(tempString);
var finalPath = tempString2.concat(".txt");
msg.filename=finalPath;

return msg;
```

---

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 11:03 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/9 "2018-06-27T11:03:54Z")

</div>

@ThanosMel So your script works if I do not include the first line `var tempString = Date().toString()` but define `tempString` as a normal string…

---

<div class="post-metadata">

### Author: ![ThanosMel](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@ThanosMel](https://discourse.nodered.org/u/ThanosMel)
#### Post date: [27 June 2018 11:05 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/10 "2018-06-27T11:05:04Z")

</div>

try the new answer and give me feedback

---

<div class="post-metadata">

### Author: ![BowenWT](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@BowenWT](https://discourse.nodered.org/u/BowenWT)
#### Post date: [27 June 2018 11:05 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/11 "2018-06-27T11:05:35Z")

</div>

BRILLIANT!! Thank you very much!! It works a treat!

---

<div class="post-metadata">

### Author: ![ThanosMel](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@ThanosMel](https://discourse.nodered.org/u/ThanosMel)
#### Post date: [27 June 2018 11:07 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/12 "2018-06-27T11:07:21Z")

</div>

Nice this forum is amazing Bowen i started with node red and js 2 weeks ago and they helped me so much.Glad i helped too.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [27 June 2018 14:10 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/13 "2018-06-27T14:10:27Z")

</div>

In the spirit of "there's always another way to do it", I like to use the `node-red-contrib-moment` node whenever I need to format time and date strings. Here is an example of how to append the current timestamp formatted as YYYYMMDD to a static filename:

> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/5927d845b0a9c9a97ef0e1a00f32932cf8af44b3.png)

Just put whatever static strings you want inside `[...]` square brackets, set the input to be the current timestamp, and use any of [moment's many formatting directives](http://momentjs.com/docs/#/displaying/format/). In your case, you would also set the Output to `msg.` `filename` -- no programming needed!

---

<div class="post-metadata">

### Author: ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)
#### Post date: [20 November 2018 10:33 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/14 "2018-11-20T10:33:43Z")

</div>

can give a simple exemple?

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [20 November 2018 10:36 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/15 "2018-11-20T10:36:49Z")

</div>

Of what? The answer to how to solve the issue that was posted is linked at the bottom of the very first post.

---

<div class="post-metadata">

### Author: ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)
#### Post date: [20 November 2018 10:40 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/16 "2018-11-20T10:40:48Z")

</div>

i mean an exemple of `node-red-contrib-moment` node.

---

<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: [20 November 2018 11:05 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/17 "2018-11-20T11:05:29Z")

</div>

> [@hathemi](#):
>
> i mean an exemple of `node-red-contrib-moment` node.

Just have a go with it, read the info panel and see what you can do

---

<div class="post-metadata">

### Author: ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)
#### Post date: [20 November 2018 11:09 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/18 "2018-11-20T11:09:43Z")

</div>

ok i will.

---

<div class="post-metadata">

### Author: ![RickW](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RickW](https://discourse.nodered.org/u/RickW)
#### Post date: [2 September 2019 07:20 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/19 "2019-09-02T07:20:26Z")

</div>

The "Moment Node" option worked well for me. I think it is an elegant solution.

---

<div class="post-metadata">

### Author: ![Tokn59](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tokn59/32/35325_2.png) [@Tokn59](https://discourse.nodered.org/u/Tokn59)
#### Post date: [13 January 2021 16:41 UTC](https://discourse.nodered.org/t/msg-filename-logging-data/1117/20 "2021-01-13T16:41:03Z")

</div>

This is great but I still have a question. After constructing this file name I need to use the Cal, Service Snapshot node. How do I pass on the path and filename in my filename definition in this node ?

So the standard is :

{  
"filename": "/config/www/somefilename.jpg"  
}

How can I modify this and use var path and filename here ?

Already tried several options such as :  
{  
"filename": "{{ msg.filename }}.jpg"  
}

Many Thanks !!!

[Next page](https://discourse.nodered.org/t/msg-filename-logging-data/1117.md?page=2)
