Syntax error Node Exec

Hello

I willed create an automatism deleted folder if folder is to old. I have a varible in global with :
-patch folder : /home/pi/Documents/ANPR/IMAGES/2023
-nbr_day : 60

If inject with node inject this command this is ok
find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +60 -exec rm -r {} ;

I willed execute this fonction with an logic beacause 2023 and 60 are variable but I have one error node function.


[{"id":"12331c71.416324","type":"inject","z":"a8416f68.4598b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":1980,"wires":[["bd2836fd.965718"]]},{"id":"bc3aa9e3.1545e8","type":"debug","z":"a8416f68.4598b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1980,"wires":[]},{"id":"bd2836fd.965718","type":"change","z":"a8416f68.4598b","name":"","rules":[{"t":"set","p":"folder","pt":"msg","to":"filebrowser_default_folder_IMAGES","tot":"global"},{"t":"set","p":"TimeImage","pt":"msg","to":"backimage","tot":"global"},{"t":"set","p":"Year","pt":"msg","to":"Years","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":1980,"wires":[["9b751a56.c5db68"]]},{"id":"9b751a56.c5db68","type":"function","z":"a8416f68.4598b","name":"","func":"msg.payload =\"find \"&msg.folder& \"/\"&msg.Year &\" -mtime +\"&msg.TimeImage &\" -exec rm {} \\;'\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":1980,"wires":[["bc3aa9e3.1545e8"]]}]

The result is : {"_msgid":"c550bbd2.c1c7b8","payload":0,"folder":"/home/pi/Documents/ANPR/IMAGES","TimeImage":15,"Year":"2023"}

with payload : 0 different to find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +60 -exec rm -r {} ;

Wher is my problem or error ?

Thank you for your help

In the function node you need to use + instead of & to create the output

msg.payload ="find " + msg.folder + "/" + msg.Year + " -mtime +" + msg.TimeImage + " -exec rm {} \;'"
return msg;

Thank you

the new result is : find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +15 -exec rm {} ;
The "" betenne {} and ; is not here, in function this symbol is in orange

Do you have a solution ?

In your original message you have this:

but in your function you did not have the -r. You just need to add that in and remove the single ' at the end.

msg.payload ="find " + msg.folder + "/" + msg.Year + " -mtime +" + msg.TimeImage + " -exec rm -r {};"

yes but the real function is :

msg.payload="find "+msg.folder+"/"+msg.Year+" -mtime +"+msg.TimeImage+" -exec rm -r {} \;"
return msg;

and the \ in the end of function doesn't in output .

You might like this approach setting and using environment variables:

I have not tested it for this scenario, automated deletion of entire directories makes me uneasy.

[{"id":"afd35866f96a7664","type":"group","z":"812b779e0d82b93f","name":"Passing parameters to a Bash script as environment variables","style":{"label":true},"nodes":["32b53ca9663a2f7a","b9ade47616c81169","fbc7d37039f81c17","3904a3d3cbccc354"],"x":94,"y":351.5,"w":572,"h":129.5},{"id":"32b53ca9663a2f7a","type":"inject","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"","props":[{"p":"folder","v":"/home/pi/testdata","vt":"str"},{"p":"mtime","v":"60","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":400,"wires":[["b9ade47616c81169"]]},{"id":"b9ade47616c81169","type":"template","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"Bash script ","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"FOLDER={{{folder}}}    #ie msg.folder\nYEAR={{{Year}}}        # msg.Year\nMTIME={{{TimeImage}}}  # msg.TimeImage\nfind $FOLDER/$YEAR -mtime +$MTIME -exec rm -r {} ;","output":"str","x":350,"y":400,"wires":[["3904a3d3cbccc354"]]},{"id":"fbc7d37039f81c17","type":"exec","z":"812b779e0d82b93f","g":"afd35866f96a7664","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Run the script","x":560,"y":400,"wires":[[],[],[]]},{"id":"3904a3d3cbccc354","type":"debug","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"Full Script","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":440,"wires":[]}]

Thank you

I have modify your respons and it's ok !

find {{{folder}}}/{{{Year}}} -mtime +{{{TimeImage}}} -exec rm -r {} \;

[{"id":"12331c71.416324","type":"inject","z":"a8416f68.4598b","name":"Delete Image ANPR to old","props":[{"p":"payload"}],"repeat":"86400","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":1980,"wires":[["bd2836fd.965718"]]},{"id":"bd2836fd.965718","type":"change","z":"a8416f68.4598b","name":"","rules":[{"t":"set","p":"folder","pt":"msg","to":"filebrowser_default_folder_IMAGES","tot":"global"},{"t":"set","p":"TimeImage","pt":"msg","to":"backimage","tot":"global"},{"t":"set","p":"Year","pt":"msg","to":"Years","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":1980,"wires":[["ca2fd52e.3dd1d8"]]},{"id":"d492e759.37c1a8","type":"exec","z":"a8416f68.4598b","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":690,"y":1980,"wires":[[],[],[]]},{"id":"ca2fd52e.3dd1d8","type":"template","z":"a8416f68.4598b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"find {{{folder}}}/{{{Year}}} -mtime +{{{TimeImage}}} -exec rm -r {} \\;","output":"str","x":560,"y":1980,"wires":[["d492e759.37c1a8"]]}]

Umm. OK, glad it's working for you!

I developed my method because I wanted to have an entire arbitrary size shell script stored within my Node-red flow.

I confess I am not very familiar with the mustache syntax which I use to set environment variables in the script so I really don't know if it's better or worse combined into a single line like that than your original approach.

Show me what the output needs to be.
Note: I’ll be tied up for the next hour

I myself am not very familiar either. But the result is functional so it suits me!

By taking a little of each answers and the ideas of each one we always arrive at our ends.

Make very sure you never run that exec command (or the other versions) when msg.folder and msg.Year are null!

find / -mtime +60 -exec rm -r {}

There's also an File Delete an other file node's
So handy :wink:

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