首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell避免出现错误消息copy-item for an existing folder

powershell避免出现错误消息copy-item for an existing folder
EN

Stack Overflow用户
提问于 2021-11-28 21:41:06
回答 1查看 50关注 0票数 0

我尝试使用-force,但仍然收到错误消息

Copy-Item :具有指定名称C:\folder2的项目已存在。

代码语言:javascript
复制
Copy-Item 'c:\folder1' -Destination 'c:\folder2' -force
EN

回答 1

Stack Overflow用户

发布于 2021-11-29 16:17:57

您可以先创建目标文件夹,然后通过将\*添加到路径来复制源文件夹中的所有内容:

代码语言:javascript
复制
# first create the destination folder if it does not already exist
$null = New-Item -Path 'c:\folder2' -ItemType Directory -Force
# then copy all from the source folder to the destination
Copy-Item -Path 'c:\folder1\*' -Destination 'c:\folder2' -Recurse -Force

通过在New-Item命令上使用switch -Force,cmdlet将返回新创建的文件夹的对象或现有文件夹的对象。

在这两种情况下,我们都不需要该输出,因此我们将使用$null =忽略它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70147688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档