这是以下问题:PowerShell将Get-ChildItem的输出连接起来
这段代码可以正常工作:
Get-ChildItem -Path "D:\Wim\TM1\TI processes" -Filter "*.vue" -Recurse -File |
Where-Object { $_.BaseName -match '^[0-9]+$' } |
ForEach-Object { ($_.FullName -split '\\')[-2,-1] -join '\' } |
Out-File D:\wim.txt
但我需要将搜索文件夹限制为仅某些文件夹,基本上是以下过滤器:(D:\Wim\TM1\TI processes\\*}vues
因此所有以结尾的子文件夹}vues
)。
如果添加该通配符条件,将不会有任何结果。没有限制,我得到正确的结果。请问这可能吗?
这样做的目的是摆脱第一个输出中的第三行(这是我的复制/粘贴),并尽量减少要查看的文件夹数量。
您可以嵌套两个Get-ChildItem
调用:
An outer Get-ChildItem -Directory -Recurse
call to filter directories of interest first,
an inner Get-ChildItem -File
call that, for each directory found, examines and processes the files of interest.
Get-ChildItem -Path "D:\Wim\TM1\TI processes" -Filter "*}vues" -Recurse -Directory |
ForEach-Object {
Get-ChildItem -LiteralPath $_.FullName -Filter "*.vue" -File |
Where-Object { $_.BaseName -match '^[0-9]+$' } |
ForEach-Object { ($_.FullName -split '\\')[-2,-1] -join '\' }
} | Out-File D:\wim.txt
Note: The assumption is that all *.vue
files of interest are located directly in each *}vues
folder.
As for what you tried:
Given that you're limiting items being enumerated to files (-File
), your directory-name wildcard pattern *}vues
never gets to match any directory names and, in the absence of files matching that pattern, returns nothing.
通常,-Recurse
从概念上讲,更干净的方法是不将通配符模式直接附加到自-Path
变量,以更好地表明该模式将在子树的每个目录中匹配。
在您的情况下,您会注意到尝试进行双重过滤,因为您还使用了-Filter
参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句