Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NodeJS - Enumerate level-0 (non-recursive) files in folder, prepared for an argument input (not used)
"use strict";
const FS = require("fs")
,PATH = require("path")
,RESOLVE = function(path){path = path.replace(/\\+/g,"/");
path = PATH.resolve(path);
path = path.replace(/\\+/g,"/");
path = path.replace(/\/\/+/g,"/");
path = path.replace(/\/+$/g,""); //remove last '/'
return path;
,ARGS = process.argv.filter(function(s){return false === /node\.exe/i.test(s) && process.mainModule.filename !== s;})
,NATURAL_COMPARE = function(g,h){var b=[],c=[];g.replace(/(\d+)|(\D+)/g,function(k,d,e){b.push([d||Infinity,e||""])});for(h.replace(/(\d+)|(\D+)/g,function(k,d,e){c.push([d||Infinity,e||""])});0<b.length&&0<c.length;){var a=b.shift();var f=c.shift();a=a[0]-f[0]||a[1].localeCompare(f[1]);if(0!==a)return a}return b.length-c.length}
,ENUMERATE_FILES = function(path){
path = RESOLVE(path); //normalize path.
return FS.readdirSync(path, {encoding:"utf8"}) //array of RELATIVE sub-paths.
.map(function(sub_path){ return path + "/" + sub_path; }) //make fully-qualified paths.
.filter(function(path){ return (true === FS.lstatSync(path).isFile()); }) //keep actual-files (ignore non-files or pointers).
console.log( ENUMERATE_FILES("./") );
process.exitCode = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.