# How to select a CSV file from a folder

**URL:** <https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683>\
**Category:** General\
**Created:** [15 May 2020 04:38 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683 "2020-05-15T04:38:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![red\_arrowhead](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@red\_arrowhead](https://discourse.nodered.org/u/red_arrowhead)\
**Post date:** [15 May 2020 04:38 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/1 "2020-05-15T04:38:23Z")

</div>

I want to know how can I select a most recent CSV file from a specific folder using Node Red? I want dynamic selection of a CSV file from a specific folder. I basically, I know the folder from which the CSV file has to be selected. After selecting the file, I am sending the CSV data to MSSQL database. Currently, I am entering the file name and path in MSSQL node. But I want to select a latest file from a specific folder? I am unable to do it. How can I proceed?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [15 May 2020 05:54 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/2 "2020-05-15T05:54:19Z")

</div>

What platofrm are you running on - Windows or Linux ?

Do you know the filename or just the directory and it will be the newest CSV file ?

I would use the exec node on linux and do a bash shell script to find and return the newest file with CSV

[http://mywiki.wooledge.org/BashFAQ/003](http://mywiki.wooledge.org/BashFAQ/003)

This assumes that you will have permission to access that file/directory

If you are on Windows 10 you could use the same (if you want to use the WSL in WIndows 10) or you could do a powershell script like this

> <https://stackoverflow.com/questions/9675658/powershell-get-childitem-most-recent-file-in-directory>

regards

Craig

---

<div class="post-metadata">

**Author:** ![red\_arrowhead](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@red\_arrowhead](https://discourse.nodered.org/u/red_arrowhead)\
**Post date:** [15 May 2020 05:58 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/3 "2020-05-15T05:58:14Z")

</div>

I am working on a Windows 10 based system. I know the file name. But the folder will be updated with new flies constantly. File name can be 'Name\_\*\*' where the \*\* will be changing part in file name. I want node red to access the folder and access the most recent file. This is what I intend to do.Is it possible?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [15 May 2020 06:00 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/4 "2020-05-15T06:00:53Z")

</div>

Yep - just pass the wildcard parameter into powershell

I suggest you get this working as a PS script first and then try to exec out to it (i have not tried it on Windows as i use Linux for NR only)

But i do know and work with PS on my local windows machine etc

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [15 May 2020 06:03 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/5 "2020-05-15T06:03:16Z")

</div>

here for example

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

---

<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:** [15 May 2020 08:22 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/6 "2020-05-15T08:22:48Z")

</div>

Is the name under your control? If so then if you made the variable bit be a timestamp (so something like Name\_200511\_1051) then you could use node-red-contrib-fsops to fetch the files in the directory and sort the names to get the most recent.  
To save having to continuously monitor the directory then use a Watch node on the directory to tell you when things change.  
However you do it watch out for the problem of picking up a file while it is still being written to. The best way is to write it with a identifiable name (with a .tmp extension for example) so you know to ignore it, and then rename it to the correct name when it is fully written.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 May 2020 09:28 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/7 "2020-05-15T09:28:42Z")

</div>

Another, cross-platform solution would be to attach the node.js standard `fs` library to a global var in settings.js then you can write a function that will read the folder. Better still, use the fs-extra npm package (which Node-RED already uses as does uibuilder).

---

<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:** [15 May 2020 09:56 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/8 "2020-05-15T09:56:44Z")

</div>

I find the [fs-ops](https://flows.nodered.org/node/node-red-contrib-fs-ops) nodes perfect for this kinda thing...

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/6020c27a58c3066465eea155c9f241afd21ede38.png)

```auto
[{"id":"6456763b.e07d78","type":"fs-ops-dir","z":"6ef1cfb5.30ff2","name":"","path":"payload","pathType":"msg","filter":"*.csv","filterType":"str","dir":"files","dirType":"msg","x":380,"y":460,"wires":[["35811671.90ebea","572a14c4.62aa3c"]]},{"id":"35811671.90ebea","type":"fs-ops-stats","z":"6ef1cfb5.30ff2","name":"","path":"payload","pathType":"msg","filename":"files","filenameType":"msg","stats":"stats","sizeType":"msg","x":540,"y":460,"wires":[["20ef5f9.b8d63a","ee6515ea.f968c8"]]},{"id":"7dbdf6ef.a73868","type":"inject","z":"6ef1cfb5.30ff2","name":"","topic":"","payload":"c:\\temp","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":460,"wires":[["6456763b.e07d78"]]},{"id":"dd9228cf.983768","type":"debug","z":"6ef1cfb5.30ff2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"combined","targetType":"msg","x":940,"y":520,"wires":[]},{"id":"572a14c4.62aa3c","type":"debug","z":"6ef1cfb5.30ff2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"files","targetType":"msg","x":520,"y":520,"wires":[]},{"id":"20ef5f9.b8d63a","type":"debug","z":"6ef1cfb5.30ff2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"stats","targetType":"msg","x":720,"y":520,"wires":[]},{"id":"ee6515ea.f968c8","type":"function","z":"6ef1cfb5.30ff2","name":"Combine and sort","func":"var combined = [];\n\nfor (var i = 0; i < msg.files.length; i++) {\n var file = msg.files[i];\n var stat = msg.stats[i];\n \n combined.push({\n path: msg.payload,\n file,\n ...stat\n })\n}\n\ncombined.sort((a, b) => b.birthtimeMs - a.birthtimeMs);\n\nmsg.combined = combined;\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":460,"wires":[["dd9228cf.983768","50034043.e2347"]]},{"id":"50034043.e2347","type":"change","z":"6ef1cfb5.30ff2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"combined[0]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":460,"wires":[["4f9068e.e12c498"]]},{"id":"4f9068e.e12c498","type":"debug","z":"6ef1cfb5.30ff2","name":"newest CSV","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1110,"y":460,"wires":[]},{"id":"25428842.245c68","type":"comment","z":"6ef1cfb5.30ff2","name":"Directory gets list of CSVs. File Stats gets their details. combine and sort puts data together and sorts. Switch node takes 1st item and puts it in payload","info":"","x":700,"y":400,"wires":[]}]

```

See flows library for more detail - [https://flows.nodered.org/flow/5f3bf35079ec128a8ddb11180da279ad](https://flows.nodered.org/flow/5f3bf35079ec128a8ddb11180da279ad)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 May 2020 13:53 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/9 "2020-05-15T13:53:59Z")

</div>

Actually, I think my own fs node would also do what is needed in this case 😁

It hasn't had a lot of TLC recently but still works fine. And if it doesn't do what is needed, raising an issue would surely prompt me to dig into the midnight oil to add something, I doubt it would be difficult.

---

<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:** [15 May 2020 14:05 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/10 "2020-05-15T14:05:01Z")

</div>

Which node are you referring to? I didn't see anything called fs-extra?

Edit ...

What I really like about fs-ops is the generous use of typedinput to allow you to build up data during the passage of `msg` and it's handling of single or array of files. Makes the flow (like above) quite simple and elegant.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 May 2020 14:49 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/11 "2020-05-15T14:49:33Z")

</div>

> [@Steve-Mcl](#):
>
> Which node are you referring to?

Was that to me?

> **[node-red-contrib-fs](https://flows.nodered.org/node/node-red-contrib-fs)**
>
> Node-Red Node that handles the host filing system.

---

<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:** [15 May 2020 15:01 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/12 "2020-05-15T15:01:33Z")

</div>

Yeah. Sorry, thought I'd replied to your message (not the general thread).

Cheers.

---

<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:** [14 July 2020 15:01 UTC](https://discourse.nodered.org/t/how-to-select-a-csv-file-from-a-folder/26683/13 "2020-07-14T15:01:37Z")

</div>

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