5

Glob documentation (I'll refer to it):

  • * Matches 0 or more characters in a single path portion
  • ? Matches 1 character
  • [...] Matches a range of characters, similar to a RegExp range. If the first character of the range is - ! or ^ then it matches any character not in the range.
  • !(pattern|pattern|pattern) Matches anything that does not match any of the patterns provided.
  • ?(pattern|pattern|pattern) Matches zero or one occurrence of the patterns provided.
  • +(pattern|pattern|pattern) Matches one or more occurrences of the patterns provided.
  • *(a|b|c) Matches zero or more occurrences of the patterns provided
  • @(pattern|pat*|pat?erN) Matches exactly one of the patterns provided
  • ** If a "globstar" is alone in a path portion, then it matches zero or more directories and subdirectories searching for matches.

Recursively exclude FILES which name begins from underscore

Solution attempt

  1. First we need to match zero or more directories (I suppose, it means "in this directory and below"). As far as I understood the documentation, ** should do it.
  2. Next we need to exclude the files which name begins from underscore. AFAIK it is [^_]*.
  3. The filename extension left. Maybe .pug will will be enough, but .+(pug) allow to scale the solution to n filename extensions like .+(pug|haml).
/Pages/Open/**/[^_]*.+(pug)

enter image description here

The EntryPoint.pug was excluded, however it's name does not begin from underscore.

🌎 Fiddle

Recursively exclude DIRECTORIES which name begins from underscore

Solution attempt

All that I know is "globstar matches zero or more directories and subdirectories searching for matches" and "[^_]" excludes underscore. Looks like it's not enough to build the correct solution. Below solution in intuitive and does not work.

/Pages/Open/[^_]**/*.+(pug)

enter image description here

🌎 Fiddle

Recursively exclude files AND directories which name begins from underscore

Can we reach this effect just by combining the solution for files and directories?

3 Answers 3

4
+50

Use ignore option of glob to exclude directories and files starting from character underscore

var glob = require('glob');

glob("**/*",{"ignore":['**/_**.pug', "**/_**/**"]}, function (err, files) {
  console.log(files);
})

// Output
[
  'Pages',
  'Pages/Open',
  'Pages/Open/EntryPoint.pug',
  'Pages/Open/Top',
  'Pages/Open/Top/EntryPoint.pug',
  'Pages/Open/Top/SubDirectory1',
  'Pages/Open/Top/SubDirectory1/NonPartial.pug',
  'test.js'
]
0
0

How about this:

/**/[^_]*/[^_]*.pug

  • starting at root
  • all directories excluding those staritng with "_"
  • all files except those starting with "_"
  • all files of type ".pug"

Im not good enough to make it recursive, but as a quick fix you can just continue duplicating it until all sub folders of subfolders are included in the expression. Im hoping this pushes you in the right direction...

also im not sure if you want to exclude a non matched subfolder of a matched parent folder, if that makes any sense.

1
  • Have you had a chance to test it out as yet Commented Aug 31, 2020 at 20:09
0

To exclude both files and directories in your example, the following will work as the respective paths are matched against the specified condition.

{/[^_]*,/[^_]*/[^_]*,/[^_]*/[^_]*/[^_]*,/[^_]*/[^_]*/[^_]*/[^_]*}/[^_]*.pug

fiddle

In curly braces, the condition to exclude directories starting with underscore is defined for every level respectively (in a comma-delimited list).


In your example, 4 levels need to be covered (0 being the root). At the 5th level, there are only files (no more directories), hence a combined condition for filename start and .pug suffix is sufficient.
  • /[^_]* level /Pages
  • /[^_]*/[^_]* level /Pages/Open
  • /[^_]*/[^_]*/[^_]* level /Pages/Open/Top
  • /[^_]*/[^_]*/[^_]*/[^_]* level /Pages/Open/Top/SubDirectory1

For each additional level you might have, an additional segment would be added to the condition.

  • /[^_]*/[^_]*/[^_]*/[^_]*/[^_]* potential level 5

For the case of excluding files only, the expression you've tried works well, it was just applied 1 level too deep. Try:
/Pages/**/[^_]*.+(pug)

instead of

/Pages/Open/**/[^_]*.+(pug)

Not the answer you're looking for? Browse other questions tagged or ask your own question.