Create a filename to write to using date

I want to create a new file every hour to write to and append to the file.
Anyone know how?

So each hour you want to create. New file and during that hour you won’t to be able to append the the end of that file. As soon as the hour changes you want to start a new file. Is that correct?

If so add the date and hour to the file name.

I would set it up so when new data came in, I would determine the hour and build the file name and use that name for the hour. The next hour you would use the file name with new name.

this flow is what i use to create a unique filename, it works for me may not be your perfect requirement but it may guide you to the right path. you may need to install simpletime contrib node, i am sure if you know javascript you can do that in a function node as well.

the inject trigger is set to a minute, you can change that to suit your requirement

the filename path syntax may change depending on your OS/environment. this flow definitely creates a file for me in C:\Temp folder.

[{"id":"5b6a9eeea7d212da","type":"template","z":"63e32070dcc833f1","name":"","field":"filename","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"c:\\\\temp\\\\{{myyear}}{{mymonthn}}{{mydom}}{{myhour}}{{myminute}}.txt","output":"str","x":660,"y":320,"wires":[["53e316334a57ebd8","acdc4d88f1b3ac90"]]},{"id":"ea8e4933026a5196","type":"simpletime","z":"63e32070dcc833f1","name":"","mydate":false,"myymd":false,"myyear":true,"mymonth":false,"mymonthn":true,"mydom":true,"mydoy":false,"myday":false,"myhourpm":false,"myhour":true,"mytime":false,"mytimes":false,"myminute":true,"myminutes":false,"mysecond":false,"mymillis":false,"myepoch":false,"myrawdate":false,"mypm":false,"x":490,"y":320,"wires":[["5b6a9eeea7d212da"]]},{"id":"53e316334a57ebd8","type":"file","z":"63e32070dcc833f1","name":"","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":820,"y":320,"wires":[["b92a098b4dc64c1e"]]},{"id":"acdc4d88f1b3ac90","type":"debug","z":"63e32070dcc833f1","name":"Filename","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"filename","targetType":"msg","statusVal":"filename","statusType":"auto","x":820,"y":260,"wires":[]},{"id":"7ac80873f445b0d9","type":"inject","z":"63e32070dcc833f1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":320,"wires":[["ea8e4933026a5196"]]},{"id":"b92a098b4dc64c1e","type":"debug","z":"63e32070dcc833f1","name":"debug 269","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1020,"y":320,"wires":[]}]

contrib nodes used node-red-contrib-simpletime

image

Thanks for answers. I found a solution. I put the following in the J:expression for the write file node.

"Log"&$moment().format("YYYY-MM-DD.HH")&".txt"

2 Likes

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