Home > Net >  How to filter out filename that end in .table.sql or a two part extension?
How to filter out filename that end in .table.sql or a two part extension?

Time:03-23

I know how to do this with one extension in PowerShell, but since my files have a two part extension this is no longer working for me:

Get-ChildItem . -Recurse | Where-Object { $_.Extension -notin @("*.table.sql")}

CodePudding user response:

The Extension property only contains the last . and whatever else trails after (.sql in your case).

You'll want to test the whole Name instead, and you'll want to use the -[not]like operator(s) for wildcard matching:

Get-ChildItem . -Recurse |Where-Object Name -notlike '*.table.sql'

If you want to include only *.sql files to begin with, use the -Filter parameter with Get-ChildItem:

Get-ChildItem . -Recurse -Filter '*.sql' |Where-Object Name -notlike '*.table.sql'

CodePudding user response:

Mathias' helpful answer solves your immediate problem.

Taking a step back, you can simplify your command by passing the wildcard expression to
Get-ChildItem's -Exclude parameter:

Get-ChildItem . -Recurse -Exclude *.table.sql

Performance caveat: While this is not only conceptually simpler, it should perform better than post-filtering with a Where-Object call, but - due to an inefficient implementation - doesn't, as of PowerShell 7.2.2 - see GitHub issue #8662.

  • Related