# Use payload or context in filename with node: Read File

**URL:** <https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094>\
**Category:** General\
**Created:** [4 January 2022 18:28 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094 "2022-01-04T18:28:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seyude](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Seyude](https://discourse.nodered.org/u/Seyude)\
**Post date:** [4 January 2022 18:28 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/1 "2022-01-04T18:28:00Z")

</div>

Hello,

Short version: How can I use the msg.payload in the filename I want to read with the node 'Read File'?

Why:  
I'm working on the following flow:

```auto
[{"id":"d944d9276f412dd6","type":"tab","label":"TESTFLOW","disabled":false,"info":"","env":[]},{"id":"156d68144591beb5","type":"inject","z":"d944d9276f412dd6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":360,"wires":[["34076bdb35c18cf9"]]},{"id":"43370ce157a1dad4","type":"file in","z":"d944d9276f412dd6","name":"","filename":"/config/GamePC/\"global.CSV\"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":350,"y":160,"wires":[["8157078afc13d0ac"]]},{"id":"34076bdb35c18cf9","type":"fs-ops-dir","z":"d944d9276f412dd6","name":"Get files in .../GamePC","path":"/config/GamePC","pathType":"str","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":360,"y":360,"wires":[["2eaf1e780dfee6bf"]]},{"id":"e3ace22bd689d0fe","type":"api-call-service","z":"d944d9276f412dd6","name":"Delete logfiles iCue","server":"acb16d44.e70d3","version":3,"debugenabled":false,"service_domain":"delete","service":"files_in_folder","entityId":"","data":"{\"folder\":\"/config/GamePC\",\"time\":10}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":410,"y":460,"wires":[[]]},{"id":"0814cdd3f53c18ad","type":"inject","z":"d944d9276f412dd6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":460,"wires":[["e3ace22bd689d0fe"]]},{"id":"b09d95adae19c0db","type":"inject","z":"d944d9276f412dd6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":160,"wires":[["43370ce157a1dad4"]]},{"id":"8157078afc13d0ac","type":"csv","z":"d944d9276f412dd6","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"Timestamp,\"CPU Package\",\"CPU Load\",\"GPU Temp #2\",\"COMMANDER PRO Exhaust\",\"COMMANDER PRO Watertemp\",\"COMMANDER PRO Intake\"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":710,"y":160,"wires":[["03b59a09d0cc6375"]]},{"id":"03b59a09d0cc6375","type":"function","z":"d944d9276f412dd6","name":"","func":"var my_array = msg.payload;\nvar last_element = my_array[my_array.length - 1];\nmsg.payload = last_element;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":160,"wires":[["7510406112651b96","482a10aa120491ba","bc4fbbe85b7d555d","b1bea7dee428b271","d55437617dfcc60d","c2a19ad0e837bcf6"]]},{"id":"2eaf1e780dfee6bf","type":"function","z":"d944d9276f412dd6","name":"Laatste .csv","func":"var my_array = msg.files;\nvar last_element = my_array[my_array.length - 1];\nmsg.payload = last_element;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":360,"wires":[["3d194345a3b09ae6"]]},{"id":"7510406112651b96","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC CPU Temp","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"CPU Package\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":60,"wires":[[]]},{"id":"482a10aa120491ba","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC CPU Load","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"CPU Load\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":120,"wires":[[]]},{"id":"bc4fbbe85b7d555d","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC GPU Temp","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"GPU Temp #2\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":180,"wires":[[]]},{"id":"b1bea7dee428b271","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC Exhaust Temp","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"COMMANDER PRO Exhaust\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1070,"y":240,"wires":[[]]},{"id":"d55437617dfcc60d","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC Water Temp","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"COMMANDER PRO Watertemp\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":300,"wires":[[]]},{"id":"c2a19ad0e837bcf6","type":"ha-entity","z":"d944d9276f412dd6","name":"GamePC Intake Temp","server":"acb16d44.e70d3","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"payload[\"COMMANDER PRO Intake\"]","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":1060,"y":360,"wires":[[]]},{"id":"3d194345a3b09ae6","type":"change","z":"d944d9276f412dd6","name":"CSV Global","rules":[{"t":"set","p":"CSV","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":360,"wires":[[]]},{"id":"acb16d44.e70d3","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/bad5baa9f891c7b7caa3238b8755e29341faf78f.png)

I have a folder were a different PC creates a .csv logfile. When the PC reboots, it creates a new file, so I'm automating the pick and read of the newest file. Everything is working, accept for the transfer of the filename from the context global.CSV to the filename after the path.  
Anyone knows how to get this done?

Thanks,

Seyude

---

<div class="post-metadata">

**Author:** ![dczysz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dczysz/32/46158_2.png) [@dczysz](https://discourse.nodered.org/u/dczysz)\
**Post date:** [4 January 2022 18:36 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/2 "2022-01-04T18:36:40Z")

</div>

You can leave the "Filename" field for the read-file node blank and pass it a `msg.filename` to use instead. So you'd just need to use a function node to whatever file name you need.

```auto
// In a function node before the read-file node
msg.filename = "/config/GamePC/" + global.get("CSV");
return msg;

```

Is that what you're asking?

---

<div class="post-metadata">

**Author:** ![Seyude](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Seyude](https://discourse.nodered.org/u/Seyude)\
**Post date:** [4 January 2022 18:44 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/3 "2022-01-04T18:44:05Z")

</div>

Legend! Didn't know that msg.filename was even a thing. Tnx mate

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 January 2022 18:49 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/4 "2022-01-04T18:49:10Z")

</div>

You should always read the nodes built in help on the side bar. Many nodes are actually well documented (the file node definitely includes this information)

---

<div class="post-metadata">

**Author:** ![Seyude](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Seyude](https://discourse.nodered.org/u/Seyude)\
**Post date:** [4 January 2022 18:53 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/5 "2022-01-04T18:53:07Z")

</div>

Yeah, I see that now and have used the help sidebar so many times before... No idea why I didn't think of it.

---

<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:** [18 January 2022 18:53 UTC](https://discourse.nodered.org/t/use-payload-or-context-in-filename-with-node-read-file/56094/6 "2022-01-18T18:53:13Z")

</div>

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