DePhoegon icon

array building - Recursive Folder & file (Nodejs)

DePhoegon | PRO | 03/05/20 11:42:24 AM UTC | 0 ⭐ | 801 👁️ | Never ⏰ | []
JavaScript |

6.29 KB

|

None

|

0 👍

/

0 👎

var fs = require('fs')
var path = require('path')
 
var root = "C:\\"
var vext = ['mp4', 'avi', 'ts', 'mov', 'wtv']
var skip = ['node_modules', '.git', 'Windows', 'ProgramData']
var system = ['sys', 'ini', 'bat', 'exe', 'dll']
var que = [] // unsorted 
var sque = [] // Folder dump array, not sorted
var csque = [] // plans for using it as a count/awareness for furthers steps
const fld = 'fold'
const fle = 'file'
var foldcnt = 0
var spider = false
var spider2 = false
var spiderd = false
 
function FilKey(slot) {
    csque = slot
    var l = slot.length
    for (a=0;a<l;a++) { 
        var path = slot[a]
        prep(path, fle)
    }
} // Starting point for file array building, needs DirKey to be triggered first
function DirKey(slot) {
    que.push(slot)
    prep(slot, fld)
} // Starting point for Directory array building
function prep(Direct, filorfol) { 
    fs.readdir(Direct, (err, files) => {
        if (err) {
            if (err.errno === -4048 || err.errno === -4058) {
                var skp = Direct.split('\\') 
                var skplst = skp.length-1
                skip.push(skp[skplst])
                return
            } else {
                console.error("Could not list the directory.", err)
                process.exit(1)
            }
        } else {
            files.forEach( (file, index) => {
                var ffile = path.join(Direct, file)
                var sys = syschk(ffile, skip, system)
                if (sys) { } 
                else if (filorfol === fld) { 
                    folder(ffile) 
                } 
                else if (filorfol === fle) { filex(ffile) }
            })
            if (filorfol === fle) { 
                let s = csque[0]
                csque.shift()
                console.log(s + '<- Shifted off of csque') 
            } else if (filorfol === fld) {
                spider2 = true
            }
        }
    })
} /* 
Uses Directory method thinking, to process 'files' and a 2nd augurment to control file or folders
Uses read Directory (permission failure) to build inaccessible folder list to skip later
Sets spider2 True, as recently accessed folder method*/
function extchk(file, ext) {
    var sfile = file.split(".")
    var last = sfile.length-1
    var control = 0
    for (s=0;s<ext.length;s++) {
        if (sfile[last] === ext[s]) {
            var control =+1
        }
    }
    if (control > 0) { return true } else { return false }
} // Checks for file extensions & returns true if found
function syschk(file, skipp, sysext) {
    var sfile = file.split(".")
    var last = sfile.length-1
    var ffile = file.split("\\")
    var flast = ffile.length-1
    var control = 0
    for (s=0;s<sysext.length;s++) {
        if (sfile[last] === sysext[s]) { 
            control = control + 1
        }}
    for (f=0;f<skipp.length;f++) {
        if (ffile[flast] === skipp[f]) {
            control = control + 1
        }}
    if (control>0) { return true } else { return false }
} // Checks for system files & inaccessible folders with arrays, & folders to be excluded with an array
function folder(fold) {
    fs.access(fold, fs.constants.X_OK, (err) => {
        if (err) { 
            console.log(err.errno)
            
        } else {
            fs.stat(fold, (error, stat) => {
                if (error) {
                    if (err.errno === -4048 || err.errno === -4058) {
                        var skp = Direct.split('\\') 
                        var skplst = skp.length-1
                        skip.push(skp[skplst])
                        return
                    } else {
                    console.error("Error stating file.", error)
                    return
                    }
                }
                if (stat.isDirectory()) {
                    if (sque.includes(fold)) { 
                        console.log('Looping found')
                        process.exit
                    } else { 
                        foldcnt = foldcnt+1
                        sque.push(fold)
                        console.log(fold + '<- Current' +'\n Number of folders -> ' + sque.length)
                        prep(fold, fld)
                    }
                }
            })
        }
    })
} // double check access right to 'file' in question, Checks if 'file' is a folder and arrays it if so
function filex(fil) {
    fs.access(fil, fs.constants.X_OK, (err) =>{
        if (err) { 
            console.log(err.errno)
        } else {
            fs.stat(fil, (error, stat) => {
                if (error) {
                    console.error("Error stating file.", error)
                    return
                }
                if (stat.isFile()) {
                    var etx = extchk(fil, vext)
                    if (etx) { que.push(fil) }
                } 
            })
        }  
    })
} // double check access right to 'file' in question, filters for wanted files via extension w/ true
function match(num, array) {
    var num2 = array.length
    if (num === num2) {  
        return true 
    } else { return false }
} // Check to see if the count matches array length, returns true if so
let spi1 = setInterval(() => {
    if (sque.length>1 && !spider && !spider2) {
        spider = match(foldcnt, sque)
    }
}, 3000) /* 
checks every 3 seconds if preset array length is 1+ long & if both Spiders control are false
sets spider true if Match is true
To reuse, will need to a prime function for folder starting 
*/
let spi2 = setInterval(() => {
    spider2 = false
    if (spider) {
        FilKey(sque)
        spiderd = true
        }
}, 5000) /* 
checks every 5 seconds, sets 2nd spider false, checks if spider is true 
enables spider death        
*/
let spidie = setInterval(() => {
    if (spiderd) {
        clearInterval(spi1)
        clearInterval(spi2)
        clearInterval(spidie)
        console.log('Stopped Spiders')
    }
}, 1000) // disables/kills spiders
 
DirKey(root)
/*
Current starting point, Likely repacable with function that can work multiple orgin points for file scrubbing
Will be segmented to allow for more advanced feature sets/controling.
*/
 
// Control Files to come, atm use of either a file or user input to build the control files.
// WIP *DePhoegon

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎