在没有 "Where" 的路径中使用通配符的 Get-ChildItem

Get-ChildItem with wild chars in path without "Where"

我想在传递给 Get-ChildItem 的路径中使用通配符。所以在示例中我想使用 C:\root\*\container 作为路径参数。但这仅在以下 1 级中搜索。如果我写 C:\root\*\*\container 那么它只会搜索下面的 2 个级别。

我尝试做类似Get-ChildItem -Path "C:\root\*\container\vc$number" "*test*.exe" -Recurse的事情 并将结果复制到特定目录中。如果我在 C:\root 中递归搜索,我会发现太多文件。如果我使用示例中给出的路径,那么我只搜索下面的 1 级,而不是在所有目录中递归搜索(甚至可以是 5 级深)。我知道我可以使用

Get-ChildItem -Path "C:\root\" "*test*.exe" -Recurse | Where { %_.FullName -like "container\vc$number" }

但我想知道我是否可以跳过使用 Where 并在路径中使用通配符。原因是我从外部文件读取路径,有些路径包含通配符(如上例),有些则不包含。所以我希望我不必编写处理路径和使用 Get-ChildItem 带/不带 Where

的函数

因此在示例中我有

C:\root\container\*test*.exe, C:\root\a\container\*test*.exe, C:\root\b\container\*test*.exe, C:\root\c\x\y\container\*test*.exe, C:\root\c\x\y\z\g\container\*test*.exe

等等。并且 C:\root\*\container 我想找到所有这些

Get-Childitem 有一个参数 Filter 可以用来过滤你想要的结果。在你的情况下(据我所知)你想在所有名为 "container" 的目录中获取文件。

首先要获取到这些目录的路径,然后获取里面的文件如下:

Get-ChildItem "C:\root" -filter "*container*" -recurse | Get-ChildItem

输出

C:\root\a\container\new.exe
C:\root\b\container\sample.exe
C:\root\c\x\y\z\g\container\anything.exe

我最后用了.Fullname让他们如上显示