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

**URL:** <https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348>\
**Category:** General\
**Created:** [2 July 2020 01:59 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348 "2020-07-02T01:59:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [2 July 2020 01:59 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/1 "2020-07-02T01:59:04Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f1b6c67fc151b7a4d9a871e82b68c24f0c0e857.jpeg) ![sdvasv](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b63e32f49c0d249a4676f51b55835f51e76c951f.jpeg)

please see images..

Thanks

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [2 July 2020 02:26 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/2 "2020-07-02T02:26:20Z")

</div>

okay I found a way.  
 ![2342342](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/089bb3fb024a439426cb0ef91674fea2ba47c33c.jpeg)  
 ![11111222](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e878e7a5a0a63329e3fee32856533af716c4389e.jpeg)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![shimbone](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shimbone/32/24815_2.png) [@shimbone](https://discourse.nodered.org/u/shimbone)\
**Post date:** [2 July 2020 03:22 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/3 "2020-07-02T03:22:24Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [2 July 2020 03:34 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/4 "2020-07-02T03:34:13Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 July 2020 08:12 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/5 "2020-07-02T08:12:08Z")

</div>

> [@Jude.Robise](#):
>
> That's what I tried in the very beginning.

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.

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [2 July 2020 08:49 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/6 "2020-07-02T08:49:11Z")

</div>

@Colin thanks for the reply. I entered only date\_now in the field since " global." is already provided. But it didn't work. 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 July 2020 10:08 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/7 "2020-07-02T10:08:02Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [7 July 2020 10:03 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/8 "2020-07-07T10:03:27Z")

</div>

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

```auto
[{"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":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [7 July 2020 10:54 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/9 "2020-07-07T10:54:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jude.Robise](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jude.robise/32/13970_2.png) [@Jude.Robise](https://discourse.nodered.org/u/Jude.Robise)\
**Post date:** [9 July 2020 01:51 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/10 "2020-07-09T01:51:43Z")

</div>

(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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2be51d84feaf12699e43b76ff865f7b043ed6e3d.jpeg)

-Thank you

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 July 2020 07:17 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/11 "2020-07-09T07:17:36Z")

</div>

Just post a very simple flow showing the problem please.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 July 2020 09:10 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/12 "2020-07-09T09:10:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 July 2020 09:11 UTC](https://discourse.nodered.org/t/access-a-global-variable-from-node-red-contrib-fs-ops-node/29348/13 "2020-07-23T09:11:03Z")

</div>

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