Access a global variable from "node-red-contrib-fs-ops" node

Dear all,

Just a quick help, How should I access global variables from this file node?

I have created a global variable already by global.set already. Just wanna get those value to node-red-contrib-fs-ops file move node to rename.

asaF sdvasv

please see images..

Thanks

okay I found a way.
2342342
11111222

[{"id":"25396bf.badcd94","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"33e06930.8b5856","type":"fs-ops-dir","z":"25396bf.badcd94","name":"","path":"C:\\Users\\user\\Documents\\Osotspa\\Database\\","pathType":"str","filter":"*","filterType":"str","dir":"files[0]","dirType":"msg","x":580,"y":280,"wires":[["567fa9ed.c8bd88"]]},{"id":"adf7ff38.da38f","type":"inject","z":"25396bf.badcd94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":280,"wires":[["33e06930.8b5856","52059c1b.6151f4"]]},{"id":"363603f0.472b6c","type":"fs-ops-move","z":"25396bf.badcd94","name":"","sourcePath":"C:\\Users\\user\\Documents\\Osotspa\\Database\\","sourcePathType":"str","sourceFilename":"files[0]","sourceFilenameType":"msg","destPath":"C:\\Users\\user\\Documents\\Osotspa\\Backup\\","destPathType":"str","destFilename":"test123","destFilenameType":"msg","link":false,"x":900,"y":280,"wires":[[]]},{"id":"52059c1b.6151f4","type":"function","z":"25396bf.badcd94","name":"rename","func":"var d = new Date();\nvar t = d.getTime();\nvar year = d.getFullYear();\nvar month = d.getMonth()+1; \nif(month.toString().length == 1) {\nmonth = '0'+month;\n}\nvar day = d.getDate();\nvar hour  = d.getHours();\nvar min = d.getMinutes();\nvar sec = d.getSeconds();\nmsg.date = t;\n \nglobal.set('date_now', year+month+day+hour+min+sec);\nreturn msg;","outputs":1,"noerr":0,"x":400,"y":220,"wires":[["f2f5fb2d.99b7a8"]]},{"id":"f2f5fb2d.99b7a8","type":"debug","z":"25396bf.badcd94","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":220,"wires":[]},{"id":"567fa9ed.c8bd88","type":"function","z":"25396bf.badcd94","name":"","func":"msg.test123 = global.get('date_now')+\".csv\";\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":280,"wires":[["363603f0.472b6c"]]}]

Another solution-- instead of putting it in a message property msg.test123 and then using that as the file name (as your solution shows), you could also access it directly with global.date_now using the node's settings (the global.get('date_now') that was in the screen shot is the syntax you'd use inside a function node)

1 Like

@shimbone thanks for the reply. global.date_now did not work.

That's what I tried in the very beginning. I could not make it work though.

-Jude

1 Like

Did you enter global.date_now in the field or did you just enter date_now as the global prefix is already there provided for you? Just the same as if you want msg.payload you select type msg and enter payload.

@Colin thanks for the reply. I entered only date_now in the field since " global." is already provided. But it didn't work. :slight_smile:

What was the error, if any, that you got?

Could you post a very simple flow showing it failing please?

1 Like

I created a separate small flow just to show you. There are two outputs to file nodes. One has msg.test123 which is working fine, second one has global.date_now, which gives an error about missing file path. I could not figure out the error.

Top flow i'm writing into the global variable.

error is this: it says no file.
"Error: ENOENT: no such file or directory, rename 'C:\Users\user\Documents\Osotspa\Database\Osotspa_Report.csv' -> 'C:\Users\user\Documents\Osotspa\Backup\2020077165238.csv'"

Thanks
-Jude

[{"id":"e30f2afe.0e0bb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"42ff9f7.7a9696","type":"fs-ops-dir","z":"e30f2afe.0e0bb8","name":"","path":"C:\\Users\\user\\Documents\\Osotspa\\Database\\","pathType":"str","filter":"*","filterType":"str","dir":"files[0]","dirType":"msg","x":340,"y":140,"wires":[["5419519e.7dc8d"]]},{"id":"72b7172b.bcb848","type":"fs-ops-move","z":"e30f2afe.0e0bb8","name":"global.date_now thing","sourcePath":"C:\\Users\\user\\Documents\\Osotspa\\Database\\","sourcePathType":"str","sourceFilename":"files[0]","sourceFilenameType":"msg","destPath":"C:\\Users\\user\\Documents\\Osotspa\\Backup\\","destPathType":"str","destFilename":"date_now","destFilenameType":"global","link":false,"x":680,"y":140,"wires":[[]]},{"id":"9ecc3efd.5b3b7","type":"inject","z":"e30f2afe.0e0bb8","name":"injecting ","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":140,"wires":[["42ff9f7.7a9696"]]},{"id":"1fd45dcf.d39ec2","type":"fs-ops-move","z":"e30f2afe.0e0bb8","name":"msg.test123 thing","sourcePath":"C:\\Users\\user\\Documents\\Osotspa\\Database\\","sourcePathType":"str","sourceFilename":"files[0]","sourceFilenameType":"msg","destPath":"C:\\Users\\user\\Documents\\Osotspa\\Backup\\","destPathType":"str","destFilename":"test123","destFilenameType":"msg","link":false,"x":670,"y":180,"wires":[[]]},{"id":"5419519e.7dc8d","type":"function","z":"e30f2afe.0e0bb8","name":"","func":"msg.test123 = global.get('date_now')+\".csv\";\nreturn msg;\n","outputs":1,"noerr":0,"x":490,"y":140,"wires":[["72b7172b.bcb848","1fd45dcf.d39ec2"]]},{"id":"4dec15a0.cebe2c","type":"inject","z":"e30f2afe.0e0bb8","name":"setting global variable","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":80,"wires":[["b26fdb57.82b728"]]},{"id":"b26fdb57.82b728","type":"function","z":"e30f2afe.0e0bb8","name":"rename","func":"var d = new Date();\nvar t = d.getTime();\nvar year = d.getFullYear();\nvar month = d.getMonth()+1; \nif(month.toString().length == 1) {\nmonth = '0'+month;\n}\nvar day = d.getDate();\nvar hour  = d.getHours();\nvar min = d.getMinutes();\nvar sec = d.getSeconds();\nmsg.date = t;\n \nglobal.set('date_now', year+month+day+hour+min+sec);\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":80,"wires":[[]]}]

That is much too complicated, why are you using a variable filename? I can't even work out what it is trying to do.
Something much simpler please.

(Oh sorry. I had to use variable file names in order to save some database .csv files according to each date and time. So I used current date and time to the file name (so it doesn't overwrite files).)

Issue here is; if I use global.set('var1', '1234'); , in a function node,

I could not use global.var1 inside the file move node to get that variable name to the file name.

Instead, I assigned that to a variable and used as a msg.payload in the file node.

dsffdfa

-Thank you

Just post a very simple flow showing the problem please.

1 Like

It looks -to me - that you are trying to move the same file to two different locations at the same time. If the first move works, the second will fail since the file will no longer be in the original folder.

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