Search code examples
javascriptnode.jsglob

How can i get a list of filename with different patterns using glob npm module


I want to get the list of filename inside multiple directories which contains file-name as a string in filename. For example

  • file-name.js
  • abcd-file-name.js
  • file-name-abcd.js
  • abcd-file-name-abcd.js

here is what i have done so far

var glob = require("glob")

glob(process.cwd() + "/directory/**/*-file-name*.*", {}, function (er, 
 files) {
  console.log(files)
})

I am only getting files which contains abcd-file-name-abcd.js


Solution

  • This should match your required solution

    var glob = require("glob")
    glob(process.cwd() + "/directory/**/?(*|*-)file-name?(-*).*", {}, function (er, 
     files) {
      console.log(files)
    })
    

    Resources: