Powershell 5复制项目目录到目录错误

时间:2018-07-09 19:49:58

标签: powershell copy-item

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

这是我的示例文件结构

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

我尝试使用{p>将所有文件和子文件夹从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以管理员身份运行

2 个答案:

答案 0 :(得分:1)

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

-强制将需要覆盖。

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

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

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

答案 1 :(得分:1)

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

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