如何过滤掉以 .table.sql 或两部分扩展名结尾的文件名?
How to filter out filename that end in .table.sql or a two part extension?
我知道如何在 PowerShell 中使用一个扩展名来执行此操作,但由于我的文件有两部分扩展名,这不再适用于我:
Get-ChildItem . -Recurse | Where-Object { $_.Extension -notin @("*.table.sql")}
Extension
属性 只包含最后一个 .
和后面的任何其他内容(.sql
在你的情况下)。
您需要测试整个 Name
,并且您需要使用 -[not]like
运算符进行通配符匹配:
Get-ChildItem . -Recurse |Where-Object Name -notlike '*.table.sql'
如果您只想包含 *.sql
个文件,请将 -Filter
参数与 Get-ChildItem
:
一起使用
Get-ChildItem . -Recurse -Filter '*.sql' |Where-Object Name -notlike '*.table.sql'
解决您眼前的问题。
退一步说,您可以通过将通配符表达式传递给
Get-ChildItem
的 -Exclude
参数来简化您的命令:
Get-ChildItem . -Recurse -Exclude *.table.sql
性能警告:虽然这不仅在概念上更简单,而且应该性能更好比 post-filtering 调用 Where-Object
,但是 - 由于 实施效率低下 - 不会 ,从 PowerShell 7.2.2 开始 - 请参阅 GitHub issue #8662。
我知道如何在 PowerShell 中使用一个扩展名来执行此操作,但由于我的文件有两部分扩展名,这不再适用于我:
Get-ChildItem . -Recurse | Where-Object { $_.Extension -notin @("*.table.sql")}
Extension
属性 只包含最后一个 .
和后面的任何其他内容(.sql
在你的情况下)。
您需要测试整个 Name
,并且您需要使用 -[not]like
运算符进行通配符匹配:
Get-ChildItem . -Recurse |Where-Object Name -notlike '*.table.sql'
如果您只想包含 *.sql
个文件,请将 -Filter
参数与 Get-ChildItem
:
Get-ChildItem . -Recurse -Filter '*.sql' |Where-Object Name -notlike '*.table.sql'
退一步说,您可以通过将通配符表达式传递给 Get-ChildItem
的 -Exclude
参数来简化您的命令:
Get-ChildItem . -Recurse -Exclude *.table.sql
性能警告:虽然这不仅在概念上更简单,而且应该性能更好比 post-filtering 调用 Where-Object
,但是 - 由于 实施效率低下 - 不会 ,从 PowerShell 7.2.2 开始 - 请参阅 GitHub issue #8662。