目标文件夹存在时的复制项目或不是

时间:2018-06-19 08:35:46

标签: powershell copy copy-item

目标文件夹存在时,定义了正确的文件复制方式here

Copy-Item 'C:\Source\*' 'C:\Destination' -Recurse -Force

如果目标文件夹不存在,源子文件夹中的某些文件将直接复制到目标,而不保留原始文件夹结构。

有没有办法让一个Copy-Item命令来解决这两种情况并保持文件夹结构?或者从Powershell要求太多了?

2 个答案:

答案 0 :(得分:0)

尝试以下操作,应该有希望解决问题

$Destination = 'c:\test5\'

New-Item -ItemType directory -Path $Destination
Copy-Item 'C:\test\*' $Destination -Recurse -Force

答案 1 :(得分:0)

您可能希望将if语句与test-path一起使用

这是我用来解决此问题的脚本

$ValidPath = Test-Path -Path c:\temp

If ($ValidPath -eq $False){

    New-Item -Path "c:\temp" -ItemType directory
    Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
}

Else {
      Copy-Item -Path "c:\temp" -Destination "c:\temp2" -force
     }