测试路径移动项目问题

Test-Path Move-Item Problems

我 运行 这个 PowerShell 脚本,它在 PowerShell 4.0 上运行良好。但我现在有 PowerShell 5.0,脚本确实可以运行,但会引发错误:

脚本:

$path = "X"
$destination = "Y"

while (Test-Path -Path $path) {
    Move-Item -Path "$path\*zip" -Destination "$destination"
    }

我得到的错误是:

Move-Item : The process cannot access the file because it is being used by another process.

问题的标题:"Test-Path Move-Item Problems" 暗示一个 cmdlet 可能会影响另一个。这对我来说没有意义,因为 Test-Path 正在检查文件夹是否存在,而 Move-Item 正在处理该文件夹中的 child 项。

就我个人而言,我 不会 对这个用例使用 while 循环,因为一旦确定路径存在,就不需要继续测试它:

if(Test-Path -Path $path){
  Move-Item -Path $path\*zip -Destination $destination
}

照做

 Move-Item -Path "$path\*zip" -Destination "$destination" -ErrorAction Ignore