Find newest file in a directory

A quick demo flow using fs-ops nodes to demonstrate how you could find the newest file in a directory.

To use...

  • Copy the flow
  • In node-red, press CTRL+I (to open the Import dialog)
  • Paste in your flow
  • Edit the inject node and set the payload to the directory of interest

Results...

The final msg.payload will contain an object with details of the newest file e.g..

{
   "path":"c:\\temp",
   "file":"c1.csv",
   "dev":4207262028,
   "mode":33206,
   "nlink":1,
   "uid":0,
   "gid":0,
   "rdev":0,
   "blksize":4096,
   "ino":7318349394751424,
   "size":827058,
   "blocks":1616,
   "atimeMs":1589535873903.5276,
   "mtimeMs":1585514722694.695,
   "ctimeMs":1589535882149.371,
   "birthtimeMs":1589535873878.613,
   "atime":"2020-05-15T09:44:33.904Z",
   "mtime":"2020-03-29T20:45:22.695Z",
   "ctime":"2020-05-15T09:44:42.149Z",
   "birthtime":"2020-05-15T09:44:33.879Z"
}

TIP: You can also filter file types (e.g. CSV files) by setting the filter field in the fs-directory node

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

Flow Info

Created 4 years, 11 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • comment (x1)
  • debug (x4)
  • function (x1)
  • inject (x1)
Other
  • fs-ops-dir (x1)
  • fs-ops-stats (x1)

Tags

  • file
  • array
  • sort
  • node-red-contrib-fs-ops
  • fs
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option