我想在传递给Get-ChildItem
的路径中使用通配符。因此,在示例中,我想使用C:\root\*\container
作为路径参数。但这仅在以下1级中搜索。如果我写C:\root\*\*\container
,那么它将只搜索下面的2个级别。
我尝试做类似Get-ChildItem -Path "C:\root\*\container\vc$number" "*test*.exe" -Recurse
的事情
并将结果复制到特定目录。如果我在C:\root
中进行递归搜索,则会发现文件过多。如果我使用示例中给出的路径,那么我仅搜索以下一级,而不是在所有目录中递归搜索(甚至可以搜索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
找到所有的人
答案 0 :(得分:1)
Get-Childitem
具有参数Filter
,可用于过滤所需的结果。在您的情况下(据我了解),您想在名为“容器”的所有目录中获取文件。
首先,您必须获取这些目录的路径,然后按如下所示获取内部文件:
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
使其显示为上方