Last active
April 11, 2017 17:17
-
-
Save roycrxtw/79344a62d1cb76a2e14e2964a3ab4326 to your computer and use it in GitHub Desktop.
Get all files in specific directory recursively. (Using node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get all files in specific directory recursively. | |
*/ | |
var fs = require('fs'); | |
var exec = require('child_process').exec; | |
var getFiles = function(path){ | |
console.log('-----------------------------------'); | |
console.log('Now on: ', path); | |
var files = fs.readdirSync(path); | |
for(var i in files){ | |
var currentFile = path + '/' + files[i]; | |
var status = fs.statSync(currentFile); | |
let output = ''; | |
if(status.isDirectory()){ | |
output += '[directory] '; | |
}else if(status.isFile()){ | |
output += '[file] '; | |
} | |
console.log(output + files[i]); | |
if(status.isDirectory()){ | |
getFiles(currentFile); | |
} | |
} | |
}; | |
var getFiles2 = function(path){ | |
exec('find ' + path, (err, stdout, stderr) => { | |
var filelist = stdout.split('\n'); | |
// further process here | |
}); | |
}; | |
getFiles('C:/alpha/testdir'); | |
getFiles2('C:/alpha/testdir'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment