Subflow - Get a recursive array of files in a folder using FS
An easy way to get files and their stats from a folder.
Requires FS assigned as a global variable to work.
I usually find many ways to do this, so I decided to create a subflow to make it easier for me.
Everything i think u need is explained in the node itself
[{"id":"442f82fa.b1dd6c","type":"subflow","name":"Folder Reader","info":"\n# Inputs\nIf a msg.path or a msg.recursive is sent, the msg will be used.\notherwise the node options will be used\n## path\nmsg.path or the path option assigns the directory to read\n## recursive\nmsg.recursive or the recursive option will indicate if a search should also be made in the folders that are found. If false, only the files in the specified folder are obtained\n## result\nAs a result, an array with the stats of all the files is obtained\n\n# (i) This node needs FS \n(is assigned on Funcion \"Read Folder\", first line, you can change it)\n","category":"","in":[{"x":120,"y":120,"wires":[{"id":"f7d97639.3a3488"}]}],"out":[{"x":700,"y":120,"wires":[{"id":"dc924f08.6c93e","port":0}]}],"env":[{"name":"setFolder","type":"str","value":"","ui":{"icon":"font-awesome/fa-file","label":{"en-US":"Folder"},"type":"input","opts":{"types":["str","env"]}}},{"name":"setRecursive","type":"bool","value":"true","ui":{"icon":"font-awesome/fa-repeat","label":{"en-US":"Recursive"},"type":"input","opts":{"types":["bool"]}}}],"color":"#DDAA99","status":{"x":340,"y":220,"wires":[{"id":"77f85900.d547a8","port":0}]}},{"id":"f7d97639.3a3488","type":"function","z":"442f82fa.b1dd6c","name":"Read Folder","func":"const fs = global.get('fs');\nvar path = (msg.path)?msg.path:(env.get(\"setFolder\"));\nvar setRecursive = (msg.recursive && typeof msg.recursive === \"boolean\")? msg.recursive : env.get(\"setRecursive\");\nnode.status({text:\"reading\",fill:\"blue\"})\nfunction getDirectoryRecursive(__dirname) {\n var files = fs.readdirSync(__dirname)\n for(var i=0; i<files.length;i++){\n var file = fs.statSync(`${__dirname}/${files[i]}`);\n file.name = files[i]\n file.path = `${__dirname}/${files[i]}`\n file.child = []\n if(file.isFile()){\n file.type = \"file\"\n files[i] = file\n }\n else if(file.isDirectory()){\n file.type = \"directory\"\n files[i] = file\n file.child=getDirectoryRecursive(file.path)\n }\n }\n return files\n}\nfunction getDirectory(__dirname) {\n var files = fs.readdirSync(__dirname)\n for(var i=0; i<files.length;i++){\n var file = fs.statSync(`${__dirname}/${files[i]}`);\n file.name = files[i]\n file.path = `${__dirname}/${files[i]}`\n if(file.isFile()){\n file.type = \"file\"\n files[i] = file\n }\n else if(file.isDirectory()){\n file.type = \"directory\"\n files[i] = file\n }\n }\n return files\n}\nmsg.payload = (setRecursive)?getDirectoryRecursive(path):getDirectory(path)\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":270,"y":120,"wires":[["dc924f08.6c93e"]]},{"id":"dc924f08.6c93e","type":"function","z":"442f82fa.b1dd6c","name":"","func":"node.status({text:\"readed\",fill:\"green\"})\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":120,"wires":[[]]},{"id":"77f85900.d547a8","type":"status","z":"442f82fa.b1dd6c","name":"","scope":null,"x":180,"y":220,"wires":[[]]},{"id":"a7819a21.461cd8","type":"subflow:442f82fa.b1dd6c","z":"4fbee581.75591c","name":"","env":[{"name":"setRecursive","value":"false","type":"bool"}],"x":380,"y":400,"wires":[["313ac9bc.b47536"]],"inputLabels":["path and recursive"],"outputLabels":["payload"],"icon":"font-awesome/fa-folder-open"},{"id":"313ac9bc.b47536","type":"debug","z":"4fbee581.75591c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":570,"y":400,"wires":[]},{"id":"ac552476.302448","type":"inject","z":"4fbee581.75591c","name":"Normal Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":360,"wires":[["a7819a21.461cd8"]]},{"id":"c94eba5e.a439b8","type":"inject","z":"4fbee581.75591c","name":"Inject by msg","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"path","v":"C:\\Users\\MyUser\\Downloads","vt":"str"},{"p":"recursive","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":440,"wires":[["a7819a21.461cd8"]]}]