复制项目不一致的行为? [英] Copy-Item inconsistent behavior?

查看:83
本文介绍了复制项目不一致的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个目录结构:

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

如果我运行命令

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

我有

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?

推荐答案

在第一种情况下目标文件夹 C:\temp\B\A 不存在,所以 Copy-Item 创建(丢失的)目标文件夹并将源文件夹的内容复制到其中.

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.

在第二种情况下目标文件夹已经存在,所以 Copy-Item 将整个源文件夹(包括文件夹本身)复制到(现有的)目标文件夹.

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.

为避免此行为,请确保在复制之前目标文件夹存在或不存在(取决于您希望将源文件夹复制到"目标还是作为"目标).使用 Test-Path 检查目标和 New-Item 的存在创建一个丢失的文件夹.

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.

这篇关于复制项目不一致的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆