我正在尝试复制嵌套在目录内的一些PDF。
这是结构。大约有100个类似于下面结构的目录
顶级文件夹
ParentFolder1
ParentFolder2
我要执行的操作是将每个Subfolder1
中的所有内容复制到ParentFolder
中。子文件夹1在每个ParentFolder
中都具有相同的名称。
我可以使用此命令获取所有文件
Get-ChildItem -Path 'C:\Temp\Powershell' -Recurse -Include *.pdf
但是当我告诉它使用此命令将文件复制过来
Get-ChildItem -Path 'C:\Temp\Powershell' -Recurse -Include *.pdf
ForEach-Object {Copy-Item $_.FullName -Destination 'C:\Temp\Destination'}
它给了我这个错误
Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:2 char:27
+ ForEach-Object {Copy-Item $_.FullName -Destination 'C:\Temp\Destinati ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
我好像已经接近了,但是我想排除所有未命名为Subfolder1
的目录,然后递归复制所有Subfolder1
内容。我尝试在-Include
语句中添加文件夹名称,但未成功。
编辑
在Tomalak的帮助下,我能够找出问题并对其进行过滤,使其仅通过每个Subfolder1
ParentFolder
Get-ChildItem -Path 'C:\Temp\Powershell' -Recurse -Include *.pdf
| Where-Object {$_.PSParentPath -like "*Subfolder1*"}
| ForEach-Object {Copy-Item $_.FullName -Destination 'C:\Temp\Destination'}
答案 0 :(得分:2)
Copy-Item
从管道中获取输入,您根本不需要使用ForEach-Object
。
Get-ChildItem -Path 'C:\Temp\Powershell' -Recurse -Include *.pdf | Copy-Item -Destination 'C:\Temp\Destination'
但是,如果需要,您仍然需要将其附加到管道(请注意|
):
Get-ChildItem -Path 'C:\Temp\Powershell' -Recurse -Include *.pdf | ForEach-Object {
Copy-Item $_.FullName -Destination 'C:\Temp\Destination'
}
您的代码在额外的一行上完全没有与以前的cmdlet的连接。