使用 Copy-Item 复制文件夹 - 不同的行为 [英] Copy folder using Copy-Item - different behaviour

查看:329
本文介绍了使用 Copy-Item 复制文件夹 - 不同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Copy-Item 将整个文件夹复制到另一个文件夹.

I want copy whole folder to another folder using Copy-Item.

我的源文件夹 c:\base 包含一些文件:

My source folder c:\base contains some files:

├───base    
│       file1.txt
│       file2.txt

我使用以下命令:

Copy-Item c:\base c:\target -recurse

如果目标文件夹 (c:\target) 存在,则命令完全按照我的意愿复制源文件夹:

if the target folder (c:\target) exists, the command copies source folder exactly as I want:

├───target
│   └───base    
│           file1.txt
│           file2.txt

如果目标文件夹不存在,该命令会创建目标文件夹(正是我想要的),但现在它只复制源文件夹的内容(没有base 文件夹):

If the target folder doesn't exist, the command creates target folder (exactly as I want), but now it copies only the content of the source folder (without the base folder):

├───target    
│       file1.txt
│       file2.txt

  • 为什么会这样?
  • 如何在不创建目标文件夹的情况下使用此复制命令(附加键?)?
  • 推荐答案

    这是一个已知错误(有人会说是功能):

    It is a known bug ( feature some would say):

    http://groups.google.com/group/microsoft.public.windows.powershell/msg/3327f2d1544e7fb6?as_umsgid=6CAEDDC7-0F1A-4B21-8FB0-E6102A16EB51@microsoft.com

    这种行为是 IMO 的人工制品提供者和对象管道powershell 使用的模型.你有习惯了这样的想法导航上下文有点奇怪与其他贝壳相比;它是显式而不是隐式,因为文件系统是众多系统之一可以使用的上下文.这带来有一些奇怪的行为,因为任何给定路径的目标是结合处理powershell 的通用语法和命令本身,而不是被完全由命令处理像 command.com/cmd.exe 这样的 shell.

    This behaviour IMO, is an artifiact of the provider and object pipeline model that powershell uses. You have to get used to the idea that the navigational context is a bit weird compared to other shells; it's explicit as opposed to implicit, since the filesystem is one of many contexts that can be used. This brings with it some weird behaviours since the targets of any given path are handled with a combination of powershell's generic grammar and the command itself, as opposed to being entirely handled by the command in shells like command.com/cmd.exe.

    这篇关于使用 Copy-Item 复制文件夹 - 不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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