【问题标题】:Powershell 5 Copy-Item Directory to Directory ErrorPowershell 5将项目目录复制到目录错误
【发布时间】:2018-07-10 03:59:34
【问题描述】:

我是 Powershell 新手,正在根据 this documentation 测试 Copy-Item 的功能。我正在尝试将文件和文件夹从子文件夹复制到它的包含文件夹中,但它不起作用。我需要使用什么命令?

这是我的示例文件结构

C:\
    Example
    |
    |__Top
        |
        |__Middle
            |
            |__Bottom

我尝试将所有文​​件和子文件夹从 Middle 复制到 Top 使用

Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recurse

但收到此错误

Copy-Item : An item with the specified name C:\Example\Top\Middle already exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (C:\Example\Top\Middle:String) 
[Copy-Item], IO
   Exception
    + FullyQualifiedErrorId : 
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Cannot overwrite the item C:\Example\Top\Middle\InTheMiddle.txt 
with itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: 
(C:\Example\Top\Middle\InTheMiddle.txt:String) [Co
   py-Item], IOException
    + FullyQualifiedErrorId : 
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : An item with the specified name C:\Example\Top\Middle\Bottom already         
exists.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: 
(C:\Example\Top\Middle\Bottom:String) [Copy-It
   em], IOException
    + FullyQualifiedErrorId : 
DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Cannot overwrite the item 
C:\Example\Top\Middle\Bottom\InTheBottom.txt with
itself.
At line:1 char:1
+ Copy-Item "C:\Example\Top\Middle" -Destination "C:\Example\Top" -Recu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: 
(C:\Example\Top\...InTheBottom.txt:String) [Copy-I
   tem], IOException
    + FullyQualifiedErrorId : 
CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Powershell 版本 5.1.17134.112 以管理员身份运行

【问题讨论】:

  • 该错误从字面上向您解释了问题。中间文件夹已经存在在顶部文件夹中。
  • 我正在尝试将中间文件夹的 contents 复制到顶部文件夹,而不是创建新文件夹或覆盖旧文件夹。我该怎么做?
  • 那你要C:\Example\Top\Middle\*.*
  • 好的,复制了Middle 中的文件,但没有复制文件夹。如何复制所有子文件夹?还有其他类似的代码吗?我在哪里可以找到它们?
  • @Aposhian 我正在尝试复制中间文件夹的内容 不。您的代码字面意思是“将文件夹 'Middle' 复制到其父文件夹 'Top'”。它已经存在的地方,因此错误。

标签: powershell copy-item


【解决方案1】:

您试图将子文件夹复制到其父文件夹。要复制“中间”文件夹的内容,您需要使用以下命令:

Copy-Item "C:\Example\Top\Middle\*" -Destination "C:\Example\Top" -Recurse

【讨论】:

【解决方案2】:

我相信这将完成您正在寻找的最简单的示例。它需要根据您的情况进行调整。

-需要应用强制来覆盖。

$source = "C:\Example\Top\Middle\*"

$destination = "C:\Example\Top\"

Copy-Item -Path $source -Destination $destination -Include *.*

【讨论】:

  • -Include *.* 是做什么的?那是隐藏文件吗?
  • 它允许您灵活地通过名称来定制您想要复制的文件而忽略扩展名,反之亦然
猜你喜欢
  • 2013-05-01
  • 2011-11-03
  • 2022-01-02
  • 1970-01-01
  • 2019-01-27
  • 2014-07-18
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多