Windows: delete files after 7 days

Hey

I am using NR on Windows (10).
My current flow creates a log file (csv) every day. This file contains various data, including from a Siemens PLC.

The log files are kept in a fixed folder. The files always have the following format: "Log File - 'date'". (example: "Log File - 2020-10-22.csv).

I am looking for a way to automatically delete the files after 7 days. However, I have not yet succeeded in this.

Someone who can give a tip?
Thank you very much for your time.

Arne

Edit:
I've already done some research on the forum and via google. However, I find nothing that matches my problem. I am not a programmer, just an interested person. I'm not asking for a ready solution :slight_smile:

Install node-red-contrib-fs-ops. Use fs-dir to get a array of the file names. Parse the names and find the ones that are a week old. Use fs-del to remove them.

Or, if you want to avoid another node installation, you can do the same with the exec node. Though that will take a bit more work. Also note that Windows has PowerShell not just the cmd.exe shell and PowerShell can do this kind of work very easily (though the initial learning curve can be offputting).

Hello

Thank you for your response. As @TotallyInformation indicates, I have already tried to use the Exec-node. Unfortunately without success. I have no knowledge of that.

I'll give it a try with @zenofmud 's suggestion. Can anyone tell me how I can possibly easily integrate the 'logic'. Involves filtering 7 day old files. Would you guys do this based on the name? Or rather based on the creation date?

Edit: I think i can 'fix' it by do some stuff with the names. But.. I don't know what's gonna happen when the month changes.. :thinking:

Thx.

I'd use the name since you have the date in the name. Figure out what today - seven days is. then parse the file name to get the date created. Compare the two, if today - 7 < filename date, delete the file. That should work for older files. I.e. let's say it doesn't run for two days but the files are still created during that time. This will delete all files with names older than seven days ago (At least that is the theory, make sure to test it)

1 Like

When you create the log file you could just parse the name of the file , add date part 7 days ago, then use new name to delete the 7 day old file.

Hello

I suspect I have found a working solution in the meantime (@zenofmud his method). The "fs-delete-nodes" are not yet connected in the flow. The debug msg does give me what I suspect to be correct. (Edit: flow does the job)
I know it is not the most optimal solution, but for me (as a non-ITprogrammer) it made sense.

Thanks again for your quick help!

Flow:

[{"id":"6808f52.910390c","type":"fs-ops-dir","z":"cf09de64.c4fd8","name":"Alarm-log","path":"*/Logging/Alarmen/","pathType":"str","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":700,"y":500,"wires":[["a8d06a8b.5ac288"]]},{"id":"139ad9b5.143da6","type":"inject","z":"cf09de64.c4fd8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":410,"y":500,"wires":[["7477f49f.df8d0c"]]},{"id":"1eacf218.4eaffe","type":"fs-ops-dir","z":"cf09de64.c4fd8","name":"PLC-log","path":"*/Logging/PLC-log/","pathType":"str","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":700,"y":540,"wires":[["c0af9e83.9b12b"]]},{"id":"52da3ec0.854cc","type":"comment","z":"cf09de64.c4fd8","name":"Verwijderen oude log-files.","info":"Delete old files. \nBewaren = Save\nVerwijderen = Delete\n\nControle = control \n","x":430,"y":460,"wires":[]},{"id":"f77bb176.233c3","type":"split","z":"cf09de64.c4fd8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1170,"y":500,"wires":[["acfc74ca.79cdc8"]]},{"id":"86456a48.432958","type":"change","z":"cf09de64.c4fd8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"files","tot":"msg"},{"t":"set","p":"controle","pt":"msg","to":"Klaar om te testen.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":500,"wires":[["f77bb176.233c3"]]},{"id":"bd988199.8f2ba","type":"function","z":"cf09de64.c4fd8","name":"Controle","func":"var bestand = msg.payload; \nvar datum = bestand.substring(0, 10);\ndatum = new Date(datum);\nmsg.controle = \"Bewaren\";\nvar filename;\nvar grensdatum = new Date(msg.grensdatum);\n\nif (grensdatum > datum){ \n    msg.controle = \"Verwijderen\";\n    filename = msg.payload; \n    \n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1480,"y":500,"wires":[["2dfe4beb.bd5154"]]},{"id":"7477f49f.df8d0c","type":"function","z":"cf09de64.c4fd8","name":"","func":"var dagen = 7; // Days you want to subtract+1\nvar datum = new Date();\nvar grens_datum = new Date(datum.getTime() - (dagen * 24 * 60 * 60 * 1000));\nvar dag = grens_datum.getDate();\nvar maand =grens_datum.getMonth()+1;\nvar jaar =grens_datum.getFullYear();\n\n\nmsg.grensdatum = grens_datum;\nreturn msg; ","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":500,"wires":[["6808f52.910390c","1eacf218.4eaffe"]]},{"id":"acfc74ca.79cdc8","type":"change","z":"cf09de64.c4fd8","name":"","rules":[{"t":"delete","p":"files","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":500,"wires":[["bd988199.8f2ba"]]},{"id":"2dfe4beb.bd5154","type":"switch","z":"cf09de64.c4fd8","name":"","property":"controle","propertyType":"msg","rules":[{"t":"eq","v":"Verwijderen","vt":"str"},{"t":"eq","v":"Bewaren","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1610,"y":500,"wires":[["3378ab9c.993fb4"],["564ee27d.0bf5dc"]]},{"id":"c0af9e83.9b12b","type":"change","z":"cf09de64.c4fd8","name":"Type: PLC","rules":[{"t":"set","p":"type","pt":"msg","to":"PLC","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":540,"wires":[["86456a48.432958"]]},{"id":"a8d06a8b.5ac288","type":"change","z":"cf09de64.c4fd8","name":"Type: Alarm ","rules":[{"t":"set","p":"type","pt":"msg","to":"ALARM","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":500,"wires":[["86456a48.432958"]]},{"id":"564ee27d.0bf5dc","type":"debug","z":"cf09de64.c4fd8","name":"Save","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1730,"y":540,"wires":[]},{"id":"3378ab9c.993fb4","type":"switch","z":"cf09de64.c4fd8","name":"","property":"type","propertyType":"msg","rules":[{"t":"eq","v":"ALARM","vt":"str"},{"t":"eq","v":"PLC","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1730,"y":500,"wires":[["46d44c48.7d9a14"],["c6b63966.6f12b8"]]},{"id":"46d44c48.7d9a14","type":"debug","z":"cf09de64.c4fd8","name":"Delete Alarm","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1890,"y":500,"wires":[]},{"id":"c6b63966.6f12b8","type":"debug","z":"cf09de64.c4fd8","name":"Delete PLC","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1890,"y":540,"wires":[]},{"id":"b42e917.367577","type":"fs-ops-delete","z":"cf09de64.c4fd8","name":"Delete PLC-log","path":"*/Logging/PLC-log/","pathType":"str","filename":"payload","filenameType":"msg","x":2120,"y":540,"wires":[[]]},{"id":"226e71e0.13202e","type":"fs-ops-delete","z":"cf09de64.c4fd8","name":"Delete Alarm-log","path":"*/Logging/Alarmen/","pathType":"str","filename":"payload","filenameType":"msg","x":2130,"y":500,"wires":[[]]}]

Arne

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