Listing files using glob patterns in nodejs

In a previous post we saw how to monitor files changes in nodejs. This was required in a cron job to check which files were changed by the cron process. I also needed to search for files in a particular directory to check which files were newly created by the process. Although this is possible using the file-state-monitor package, I needed the ability to use glob patterns while searching.

A quick search got me to the file-set package. This lets you provide a list of file patterns as input, returning a file-set instance containing the expanded patterns, split into separate lists of files, dirs and notExisting. The package is extremely small and gets the work done in a few lines of code.

Installation is via npm.

npm install file-set --save

Usage is short and simple.

const FileSet = require('file-set');
files = new FileSet('*');
console.log(files);

This will return all the files and directories in the current directory.

E:\localhost\test\nodejs>node file-set.js
FileSet {
  files:
   [ 'client_secret.json',
     'file-monitor.js',
     'file-set.js',
     'fuzzball.js',
     'gmail.js',
     'gmail0.js',
     'json-server.js',
     'rets.js',
     'states.json',
     'url.txt' ],
  dirs:
   [ 'codediesel/',
     'credentials/',
     'Express/',
     'flickr/',
     'Hello World/',
     'node_modules/' ],
  notExisting: [] }

You can read the list of files or directories as below.

files.files.forEach((file) => {
  console.log(file);
});
 
files.dirs.forEach((dir) => {
  console.log(dir);
});

The important line in the code is the constructor which accepts the glob pattern.

files = new FileSet('*');

You can for example search for only ‘.js’ files in the ‘Express’ directory. Note that the search does not go recursively into sub-directories.

files = new FileSet('Express/*.js');

Search for ‘json’ files in directories with names starting with a ‘c’.

files = new FileSet('c*/*.json');
E:\localhost\test\nodejs>node file-set.js
[ 'codediesel/package.json', 'credentials/gmail-nodejs.json' ]

The glob patterns can be a string or an array so that you can search for multiple file patterns.

pattern = [
            'Express/*.js',
            'c*/*.json'
          ];
 
files = new FileSet(pattern);
E:\localhost\test\nodejs>node file-set.js
[ 'Express/Express.js',
  'codediesel/package.json',
  'credentials/gmail-nodejs.json' ]

If you list a set of files and they are not found that the ‘notExisting’ array is filled with those files. Say we want to search for a file named ‘package_backup.json’ in the ‘Express’ directory. If it is not found then the file is listed in the ‘notExisting’ section. This means that the file is ‘non-existant. This alllows you to quickly see which files or file patterns are not available.

files = new FileSet('Express/package_back.json');
E:\localhost\test\nodejs>node file-set.js
FileSet {
  files: [],
  dirs: [],
  notExisting: [ 'Express/package_back.json' ] }

You can also add a pattern later.

pattern = [
            'Express/*.js',
            'c*/*.json'
          ];
 
 
files = new FileSet(pattern);
files.add('Express/*.md');
console.log(files);
E:\localhost\test\nodejs>node file-set.js
FileSet {
  files:
   [ 'Express/Express.js',
     'codediesel/package.json',
     'credentials/gmail-nodejs.json',
     'Express/README.md' ],
  dirs: [],
  notExisting: [] }

In closing, the ‘file-set’ package allows you to quickly look for files that either exist or do not exist using glob patterns.

Leave a Reply

Your email address will not be published. Required fields are marked *