Msg.filename logging data

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

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”;

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

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,

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

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

should be

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

Or, use / instead:

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.
1 Like

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":[]}]

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?

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;

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

try the new answer and give me feedback

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

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.

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

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. In your case, you would also set the Output to msg. filename -- no programming needed!

2 Likes

can give a simple exemple?

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

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

ok i will.

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

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 !!!