PowerShell - 如何处理正则表达式管道选择的文件夹中的文件
PowerShell - How to process files in folders selected by regex pipeline
我使用以下管道 select 特定文件夹:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
上面给出了名称中没有下划线的文件夹,这就是我想要的,到目前为止一切都很好。
但是当我将结果通过管道传输到另一个 Get-ChildItem
时,我得到一个 BindingException 错误:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' } | gci *.pdf
gci : The input object cannot be bound to any parameters for the command either because the command does not take
pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (Book Folder B:PSObject) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetChildItemCommand
如何处理上述管道输出的每个文件夹中的文件。例如,如果一个文件有 pdf
扩展名,我想为它调用 Move-Item
命令。
更改最后一个管道对象
来自gci *.pdf
至Get-childitem -Filter *.pdf
但我建议您使用以下行优化现有行:
gci -path C:\Folder\Path\* -Filter *.pdf | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
以下代码片段输出当前文件夹下 一级 子文件夹中所有 .pdf
文件的完全限定名称他们的 完整路径 中没有下划线。 (输出文件名 可以 包含下划线)。
Get-ChildItem -Path . |
Where-Object { $_.PsIsContainer -and $_.Fullname -notmatch '_' } |
Get-ChildItem -Filter *.pdf |
ForEach-Object {
<# do anything with every file object instead of just outputting its FullName <##>
$_.FullName
}
您需要在第二个 gci
中使用 -Filter
关键字相对于其允许的位置 2(注意位置 1 专用于 -Path
参数)。
如需进一步解释和潜在 improvements/optimalisations,请阅读 Get-ChildItem
as well as Get-ChildItem
for FileSystem。
我使用以下管道 select 特定文件夹:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
上面给出了名称中没有下划线的文件夹,这就是我想要的,到目前为止一切都很好。
但是当我将结果通过管道传输到另一个 Get-ChildItem
时,我得到一个 BindingException 错误:
gci -path * | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' } | gci *.pdf
gci : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
+ CategoryInfo : InvalidArgument: (Book Folder B:PSObject) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetChildItemCommand
如何处理上述管道输出的每个文件夹中的文件。例如,如果一个文件有 pdf
扩展名,我想为它调用 Move-Item
命令。
更改最后一个管道对象
来自gci *.pdf
至Get-childitem -Filter *.pdf
但我建议您使用以下行优化现有行:
gci -path C:\Folder\Path\* -Filter *.pdf | ? { $_.PsIsContainer -and $_.Fullname -notmatch '_' }
以下代码片段输出当前文件夹下 一级 子文件夹中所有 .pdf
文件的完全限定名称他们的 完整路径 中没有下划线。 (输出文件名 可以 包含下划线)。
Get-ChildItem -Path . |
Where-Object { $_.PsIsContainer -and $_.Fullname -notmatch '_' } |
Get-ChildItem -Filter *.pdf |
ForEach-Object {
<# do anything with every file object instead of just outputting its FullName <##>
$_.FullName
}
您需要在第二个 gci
中使用 -Filter
关键字相对于其允许的位置 2(注意位置 1 专用于 -Path
参数)。
如需进一步解释和潜在 improvements/optimalisations,请阅读 Get-ChildItem
as well as Get-ChildItem
for FileSystem。