Get-ChildItem and Copy-Item explanation

梦想的初衷 提交于 2019-12-14 03:45:53

问题


Why does

gci $from -Recurse | copy-item -Destination  $to -Recurse -Force -Container

not behave in the same way as

copy-item $from $to -Recurse -Force

?

I think it should be the same, but somehow it's not. Why?


回答1:


You are not looping over each item in the collection of files/folders, but passing the last value to the pipe. You need to use Foreach-item or % to the Copy-Item command. From there, you also do not need the second -recurse switch as you already have every item in the GCI.

try this:

gci $from -Recurse | % {copy-item -Path $_ -Destination  $to -Force -Container }



回答2:


Here is what worked for me

Get-ChildItem -Path .\ -Recurse -Include *.txt | %{Join-Path -Path $_.Directory -ChildPath $_.Name } | Copy-Item -Destination $to



回答3:


This works for me:

Get-ChildItem 'c:\source\' -Recurse | % {Copy-Item -Path $_.FullName -Destination 'd:\Dest\' -Force -Container }



回答4:


To loop all items, this should work:

gci -Path $from -Recurse | % { $_ | copy-item -Destination  $to -Force -Container}

Just do the foreach and pipe each item again.




回答5:


The switches are wrong. They should be:

gci -Path $from -Recurse | copy-item -Destination  $to -Force -Container


来源:https://stackoverflow.com/questions/21750311/get-childitem-and-copy-item-explanation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!