Select files using include and exclude array of recursive glob patterns

Viewed 639

I've been given two file glob parameters in JSON, include and exclude, in the following format:

{
    include: ['**/*.md', '**/swagger/*.json', '**/*.yml', 'somedir/*.yml'],
    exclude: ['**/obj/**', 'otherdir/**', '**/includes/**']
}

I'm tasked with walking a directory tree to select files according to the include and exclude rules in these formats; this has to be written as a Powershell script.

I've been trying to find a built-in command that supports the double-asterisk, recursive file glob pattern; additionally, since Powershell is converting the JSON to an object, it would be nice if the command parameters could accept an array as input.

I've looked at Get-ChildItem, but I'm not sure that I can mimic the glob resolution behavior using -include, -exclude, and/or -filter. I've also looked at Resolve-Path, but I'm not sure if the wildcards will work correctly (and I might have to manually exclude paths).

How can I select paths using multiple recursive wildcard file globs in Powershell while excluding other globs? Is there a Powershell command that supports this?

Thank you!

EDIT: In these glob patterns, the single asterisk is a regular wildcard. The double asterisk (**), however, is a known standard which denotes a recursive directory search.

For example: the pattern dir1/*/file.txt would match:

dir1/dir2/file.txt
dir1/dir3/file.txt

...but not:

dir1/dir2/dir3/file.txt

The pattern dir1/**/file.txt would match everything that the above selector would, but it would also match:

dir1/dir3/dir4/file.txt
dir1/dir7/dir9/dir23/dir47/file.txt

and so on. So, an exclude glob pattern like **/obj/** basically means "exclude anything found in any obj folder found at any point in the directory hierarchy, no matter how deep".

0 Answers
Related