Collect filenames with Node.js

Recursively generate a list of files located in a directory.

Node.js can help maintain doc experiences through automation. You can use it to:

  • Detect file changes in source repositories.

  • Fix pathname issues in doc source files

  • Repair broken (or incomplete) markup

  • Update pipeline config files

  • Generate index pages, tag clouds, and other supporting content.

Tasks like these frequently require a list of files in a given directory, along with any child directories.

Here’s one way to generate such a list:

// rudimentary command line handling.
var strFilePath = process.argv[ 2 ];
if ( ( strFilePath == null ) || ( strFilePath == "" ) || 
     ( strFilePath == undefined ) ) {
   console.log( "Declaration error: Target folder not specified." );
   return;
}

// Normalize and verify the input parameter value
var p = require( "path" );
var strFilePath = p.resolve( strFilePath ); 
var fs = require( "fs" );
var pathStat = fs.statSync( strFullFilename );
if ( pathStat.isDirectory() == false ) {
   console.log( "Declaration error: '" + 
                strFullFilename + "' is not a folder." );
   return;
}

// Prepare the result object
var oResults = new Object;
oResults.paths = [];
oResults.paths.push( strFilePath );
oResults.files = [];

// Scan every folder found; make sure we can them all
for ( var xDir = 0; xDir < ( oResults.paths.length ); xDir++ ) {
  var sCurrDir = oResults.paths[ xDir ];
  var aThisDir = fs.readdirSync( sCurrDir );

  for ( var xThisFile = 0; xThisFile < ( aThisDir.length ); xThisFile++ ) {

     var strFullFilename = sCurrDir + "/" + aThisDir[ xThisFile ];
     var xTFStat = fs.statSync( strFullFilename );

     if ( xTFStat.isFile() ) {
       oResults.files.push( strFullFilename );
     } else { if ( xTFStat.isDirectory() ) {
       oResults.paths.push( strFullFilename );
     } else {
       console.log( "Skipping ", strFullFilename, "; not a file or folder." );
     } }
  } 
}

console.log( JSON.stringify( oResults, undefined, 3 ) );

This example:

  1. Verifies the parameter value supplied by the caller. Unless a valid directory is provided, an error appears and the script ends.

  2. Scans the target directory and tests each file tested.

  3. Adds filenames to the files property array and child directories to the paths property array.

    Because the outer loop relies on paths, child directories are processed.

    You’ll notice the file evaluation block includes an else for unexpected results.

When the outer loop completes, the oResults object is ready for the next step. here, the results are logged to the console.

Alternate approach

Mind you, there are easier ways to generate a list of files, including a simple shell command:

$ find ~/projects/mydocsite/_site -name "*.html" > filelist.txt

In many cases, this might be a preferred solution.

If you need additional processing, the earlier approach can help get you started.

Vital statistics

  • 15 Feb 2024: Migrated to new site and style
  • Lasted tested February 2017:
    • Node: v7.4.0
  • 4 Feb 2017: First post