Copy-Item inconsistent behavior?

↘锁芯ラ 提交于 2020-01-14 10:32:42

问题


Consider this directory structure:

C:\temp\A\file.txt
C:\temp\B

If I run the command

Copy-Item "C:\temp\A" "C:\temp\B\A" -Recurse -Force -ErrorAction Stop

I have

C:\temp\A\file.txt
C:\temp\B\A\file.txt

If, starting from this new situation, I run the same command a second time, I end up with

C:\temp\A\file.txt
C:\temp\B\A\file.txt
C:\temp\B\A\A\file.txt

Why is the result different although I run the same command?


回答1:


In the first case the destination folder C:\temp\B\A doesn't exist, so Copy-Item creates the (missing) destination folder and copies the content of the source folder to it.

In the second case the destination folder already exists, so Copy-Item copies the entire source folder (including the folder itself) to the (existing) destination folder.

To avoid this behavior make sure the destination folder either does or does not exist before copying (depending on whether you want the source folder copied "to" the destination or "as" the destination). Use Test-Path to check for the existence of the destination and New-Item to create a missing folder.



来源:https://stackoverflow.com/questions/37392396/copy-item-inconsistent-behavior

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