【发布时间】: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