Advertisement
DePhoegon

array building - Recursive Folder & file (Nodejs)

Mar 5th, 2020
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs')
  2. var path = require('path')
  3.  
  4. var root = "C:\\"
  5. var vext = ['mp4', 'avi', 'ts', 'mov', 'wtv']
  6. var skip = ['node_modules', '.git', 'Windows', 'ProgramData']
  7. var system = ['sys', 'ini', 'bat', 'exe', 'dll']
  8. var que = [] // unsorted
  9. var sque = [] // Folder dump array, not sorted
  10. var csque = [] // plans for using it as a count/awareness for furthers steps
  11. const fld = 'fold'
  12. const fle = 'file'
  13. var foldcnt = 0
  14. var spider = false
  15. var spider2 = false
  16. var spiderd = false
  17.  
  18. function FilKey(slot) {
  19.     csque = slot
  20.     var l = slot.length
  21.     for (a=0;a<l;a++) {
  22.         var path = slot[a]
  23.         prep(path, fle)
  24.     }
  25. } // Starting point for file array building, needs DirKey to be triggered first
  26. function DirKey(slot) {
  27.     que.push(slot)
  28.     prep(slot, fld)
  29. } // Starting point for Directory array building
  30. function prep(Direct, filorfol) {
  31.     fs.readdir(Direct, (err, files) => {
  32.         if (err) {
  33.             if (err.errno === -4048 || err.errno === -4058) {
  34.                 var skp = Direct.split('\\')
  35.                 var skplst = skp.length-1
  36.                 skip.push(skp[skplst])
  37.                 return
  38.             } else {
  39.                 console.error("Could not list the directory.", err)
  40.                 process.exit(1)
  41.             }
  42.         } else {
  43.             files.forEach( (file, index) => {
  44.                 var ffile = path.join(Direct, file)
  45.                 var sys = syschk(ffile, skip, system)
  46.                 if (sys) { }
  47.                 else if (filorfol === fld) {
  48.                     folder(ffile)
  49.                 }
  50.                 else if (filorfol === fle) { filex(ffile) }
  51.             })
  52.             if (filorfol === fle) {
  53.                 let s = csque[0]
  54.                 csque.shift()
  55.                 console.log(s + '<- Shifted off of csque')
  56.             } else if (filorfol === fld) {
  57.                 spider2 = true
  58.             }
  59.         }
  60.     })
  61. } /*
  62. Uses Directory method thinking, to process 'files' and a 2nd augurment to control file or folders
  63. Uses read Directory (permission failure) to build inaccessible folder list to skip later
  64. Sets spider2 True, as recently accessed folder method*/
  65. function extchk(file, ext) {
  66.     var sfile = file.split(".")
  67.     var last = sfile.length-1
  68.     var control = 0
  69.     for (s=0;s<ext.length;s++) {
  70.         if (sfile[last] === ext[s]) {
  71.             var control =+1
  72.         }
  73.     }
  74.     if (control > 0) { return true } else { return false }
  75. } // Checks for file extensions & returns true if found
  76. function syschk(file, skipp, sysext) {
  77.     var sfile = file.split(".")
  78.     var last = sfile.length-1
  79.     var ffile = file.split("\\")
  80.     var flast = ffile.length-1
  81.     var control = 0
  82.     for (s=0;s<sysext.length;s++) {
  83.         if (sfile[last] === sysext[s]) {
  84.             control = control + 1
  85.         }}
  86.     for (f=0;f<skipp.length;f++) {
  87.         if (ffile[flast] === skipp[f]) {
  88.             control = control + 1
  89.         }}
  90.     if (control>0) { return true } else { return false }
  91. } // Checks for system files & inaccessible folders with arrays, & folders to be excluded with an array
  92. function folder(fold) {
  93.     fs.access(fold, fs.constants.X_OK, (err) => {
  94.         if (err) {
  95.             console.log(err.errno)
  96.            
  97.         } else {
  98.             fs.stat(fold, (error, stat) => {
  99.                 if (error) {
  100.                     if (err.errno === -4048 || err.errno === -4058) {
  101.                         var skp = Direct.split('\\')
  102.                         var skplst = skp.length-1
  103.                         skip.push(skp[skplst])
  104.                         return
  105.                     } else {
  106.                     console.error("Error stating file.", error)
  107.                     return
  108.                     }
  109.                 }
  110.                 if (stat.isDirectory()) {
  111.                     if (sque.includes(fold)) {
  112.                         console.log('Looping found')
  113.                         process.exit
  114.                     } else {
  115.                         foldcnt = foldcnt+1
  116.                         sque.push(fold)
  117.                         console.log(fold + '<- Current' +'\n Number of folders -> ' + sque.length)
  118.                         prep(fold, fld)
  119.                     }
  120.                 }
  121.             })
  122.         }
  123.     })
  124. } // double check access right to 'file' in question, Checks if 'file' is a folder and arrays it if so
  125. function filex(fil) {
  126.     fs.access(fil, fs.constants.X_OK, (err) =>{
  127.         if (err) {
  128.             console.log(err.errno)
  129.         } else {
  130.             fs.stat(fil, (error, stat) => {
  131.                 if (error) {
  132.                     console.error("Error stating file.", error)
  133.                     return
  134.                 }
  135.                 if (stat.isFile()) {
  136.                     var etx = extchk(fil, vext)
  137.                     if (etx) { que.push(fil) }
  138.                 }
  139.             })
  140.         }  
  141.     })
  142. } // double check access right to 'file' in question, filters for wanted files via extension w/ true
  143. function match(num, array) {
  144.     var num2 = array.length
  145.     if (num === num2) {  
  146.         return true
  147.     } else { return false }
  148. } // Check to see if the count matches array length, returns true if so
  149. let spi1 = setInterval(() => {
  150.     if (sque.length>1 && !spider && !spider2) {
  151.         spider = match(foldcnt, sque)
  152.     }
  153. }, 3000) /*
  154. checks every 3 seconds if preset array length is 1+ long & if both Spiders control are false
  155. sets spider true if Match is true
  156. To reuse, will need to a prime function for folder starting
  157. */
  158. let spi2 = setInterval(() => {
  159.     spider2 = false
  160.     if (spider) {
  161.         FilKey(sque)
  162.         spiderd = true
  163.         }
  164. }, 5000) /*
  165. checks every 5 seconds, sets 2nd spider false, checks if spider is true
  166. enables spider death        
  167. */
  168. let spidie = setInterval(() => {
  169.     if (spiderd) {
  170.         clearInterval(spi1)
  171.         clearInterval(spi2)
  172.         clearInterval(spidie)
  173.         console.log('Stopped Spiders')
  174.     }
  175. }, 1000) // disables/kills spiders
  176.  
  177. DirKey(root)
  178. /*
  179. Current starting point, Likely repacable with function that can work multiple orgin points for file scrubbing
  180. Will be segmented to allow for more advanced feature sets/controling.
  181. */
  182.  
  183. // Control Files to come, atm use of either a file or user input to build the control files.
  184. // WIP *DePhoegon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement