How to move multiple files with `fs-ops` nodes?

I have (yet) another problem when I am wanting to move multiple files from a to b.

I have these files in a directory (I just realised there is a better way of doing it, but it will still require the same mechanics to work anyway).

There is one file: last_alive.db which is a heart beat file and it is updated every x seconds.
When the machine reboots, it moves that file to a file called: Rebooted at xxxxxxx.

As you can see it is becoming cluttered (but that aside) every week it will move any Rebooted at ...... files to a directory which is the year and week number.

I've got the directory making working, but not the moving.

This is the node (with settings):
(Given the global variables are set)

[{"id":"823d9015.e08be","type":"fs-ops-move","z":"675e227d.d158b4","name":"","sourcePath":"source","sourcePathType":"msg","sourceFilename":"Reboot*","sourceFilenameType":"str","destPath":"destination","destPathType":"msg","destFilename":"*","destFilenameType":"str","link":false,"x":790,"y":2080,"wires":[["ff10ceb0.1de5"]]}]

But getting the file name part has me stumped.

Hi.

Few questions...

  • What is msg.source set to
  • what is msg.destination set to
  • is the file really called Reboot*?

my guess is you are trying to wild card this file name?

I'm not certain fs-ops move node supports wildcard file names - but the directory node does.

You'll likely have to use the directory node to get a list of files in msg.source then run through the array - then create a new msg with the file name set into a msg property then send that to the move node

Because I have nowt else to do :wink:

[{"id":"c083b11f.d977e","type":"fs-ops-dir","z":"3aef9fe3.fda76","name":"get list of files matching Reboot*","path":"sourcePath","pathType":"msg","filter":"Reboot*","filterType":"str","dir":"payload","dirType":"msg","x":590,"y":260,"wires":[["bc4d99d9.c71808"]]},{"id":"2d1efb4c.458034","type":"inject","z":"3aef9fe3.fda76","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":260,"wires":[["4a18d5a3.4c13cc"]]},{"id":"f368ea49.769748","type":"fs-ops-move","z":"3aef9fe3.fda76","name":"","sourcePath":"sourcePath","sourcePathType":"msg","sourceFilename":"payload","sourceFilenameType":"msg","destPath":"destinationPath","destPathType":"msg","destFilename":"payload","destFilenameType":"msg","link":false,"x":660,"y":320,"wires":[[]]},{"id":"4a18d5a3.4c13cc","type":"change","z":"3aef9fe3.fda76","name":"","rules":[{"t":"set","p":"sourcePath","pt":"msg","to":"c:\\temp","tot":"str"},{"t":"set","p":"destinationPath","pt":"msg","to":"c:\\temp2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["c083b11f.d977e"]]},{"id":"bc4d99d9.c71808","type":"split","z":"3aef9fe3.fda76","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":510,"y":320,"wires":[["d7ddfa7e.1755a8","f368ea49.769748"]]},{"id":"d7ddfa7e.1755a8","type":"debug","z":"3aef9fe3.fda76","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":380,"wires":[]}]

PS - I updated the title of your thread to better explain your requirement (to help others when looking for similar solution)

2 Likes

Ah! split!

(At time of posting it still isn't working, but I am looking at it because I probably have something set wrong)

Further reading shows me that I need to use msg.payload to carry the names of the files.
Not msg.files. :frowning:

Reading the docs I saw that the file names were saved to something which is compatible with other fs-ops nodes.

List of file names is stored in an array in the specified property. This array is suitable for passing to other fs-op nodes for further operations.

But I didn't think to put the split node between them. (T'would be nice if that was explained better.)

Anyway.

Yes. The file/s are called Rebooted at YYYY-dd-mm HHmmss.txt

I've changed some of the names while learning/reading stuff and because I was shown how to use the template node correctly.

Names:
directory /media/pi/06BB-C87D/logs/reboot/2020-12/
destination /media/pi/06BB-C87D/logs/reboot/2020-12/
path /media/pi/06BB-C87D/logs/
payload /media/pi/06BB-C87D/logs/reboot/

This is the flow as it stands now:

[{"id":"9b37182c.7af298","type":"delay","z":"675e227d.d158b4","name":"Delay","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":570,"y":2150,"wires":[["101f7e8a.bf1319"]]},{"id":"101f7e8a.bf1319","type":"split","z":"675e227d.d158b4","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":780,"y":2150,"wires":[["823d9015.e08be","5beb0e3d.de20b"]]},{"id":"4041d2b2.18dad4","type":"function","z":"675e227d.d158b4","name":"Build stuff","func":"msg.destination = msg.payload + msg.directory;\nreturn msg;","outputs":1,"noerr":0,"x":790,"y":2030,"wires":[["f38ba075.85be2","d5e9569e.50cea","c60b20e3.1bfec8","9b37182c.7af298"]]},{"id":"c60b20e3.1bfec8","type":"fs-ops-mkdir","z":"675e227d.d158b4","name":"","path":"payload","pathType":"msg","dirname":"directory","dirnameType":"msg","mode":"777","fullpath":"directory","fullpathType":"msg","x":600,"y":2110,"wires":[["dccc27aa.d4186"]]},{"id":"d5e9569e.50cea","type":"debug","z":"675e227d.d158b4","name":"IN","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":2070,"wires":[]},{"id":"823d9015.e08be","type":"fs-ops-move","z":"675e227d.d158b4","name":"","sourcePath":"payload","sourcePathType":"msg","sourceFilename":"files","sourceFilenameType":"msg","destPath":"destination","destPathType":"msg","destFilename":"files","destFilenameType":"msg","link":false,"x":940,"y":2150,"wires":[["ff10ceb0.1de5"]]},{"id":"5beb0e3d.de20b","type":"debug","z":"675e227d.d158b4","name":"1d","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":2190,"wires":[]},{"id":"570a0b23.860d0c","type":"fs-ops-dir","z":"675e227d.d158b4","name":"","path":"payload","pathType":"msg","filter":"Rebooted at*","filterType":"str","dir":"files","dirType":"msg","x":580,"y":2030,"wires":[["3c25455c.e66b0a","4041d2b2.18dad4"]]},{"id":"dccc27aa.d4186","type":"debug","z":"675e227d.d158b4","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":780,"y":2110,"wires":[]},{"id":"ff10ceb0.1de5","type":"debug","z":"675e227d.d158b4","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1090,"y":2150,"wires":[]},{"id":"791399b9.493ee","type":"template","z":"675e227d.d158b4","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{path}}}reboot/","output":"str","x":350,"y":2030,"wires":[["74e6ce8a.becc68","570a0b23.860d0c"]]},{"id":"3c25455c.e66b0a","type":"debug","z":"675e227d.d158b4","name":"OUT","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":780,"y":1990,"wires":[]},{"id":"8ed6e064.5d98e","type":"change","z":"675e227d.d158b4","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"directory","tot":"msg"},{"t":"set","p":"path","pt":"msg","to":"log_paths","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":1990,"wires":[["791399b9.493ee","8921a9ee.bfb6d8"]]},{"id":"74e6ce8a.becc68","type":"debug","z":"675e227d.d158b4","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":1950,"wires":[]},{"id":"c99fea29.aeaf1","type":"moment","z":"675e227d.d158b4","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-ww/","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":390,"y":1990,"wires":[["8ed6e064.5d98e"]]},{"id":"8921a9ee.bfb6d8","type":"debug","z":"675e227d.d158b4","name":"RAW","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":780,"y":1950,"wires":[]},{"id":"efce41db.b9d1c","type":"inject","z":"675e227d.d158b4","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":2260,"wires":[["c99fea29.aeaf1"]]}]

Hi, you code doesnt work because split operates on msg.payload (not msg.files)

The code I posted works - try building around that instead.

[edit] - I think you already realize that. :slight_smile:

Yeah, I realised that after putting in the split node and it still not working.

(Typically)

I then looked at the split node and read how it only works on msg.payload.

Yeah, I am moving things around to free up msg.payload so it can carry the file names.

1 Like

Thanks @Steve-Mcl

Yeah, now it is working.

I can't believe how stupid I am and how many times I had to go over the nodes - even after re-making the names - to get rid of mistakes.

I guess it will just have to chalked up to learning.

1 Like

To be even smarter I now added another node after the fs-ops-directory node thus:

[{"id":"518603eb.e2199c","type":"switch","z":"b95543c8.1478f8","name":"","property":"payload[0]","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":680,"y":370,"wires":[["384f0968.70a6ce"]]}]

So if there are no files it doesn't go further.

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