powershell避免错误消息复制现有文件夹的项目



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

复制项:已存在具有指定名称C:\folder2的项存在。

Copy-Item 'c:folder1' -Destination 'c:folder2' -force

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

# 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命令上使用开关-Force,cmdlet返回新创建文件夹的对象现有文件夹的对象
在这两种情况下,我们都不需要该输出,因此我们将使用$null =忽略它

最新更新