【问题标题】:How do you move files/folders across volumes with Powershell?如何使用 Powershell 跨卷移动文件/文件夹?
【发布时间】:2011-05-26 12:58:18
【问题描述】:

我尝试使用 PowerShell 移动文件夹

move-item  c:\test c:\test2

有效,但是

move-item c:\test  \\192.168.1.50\c$\test2

不告诉我

Move-Item : 源和目标 路径必须具有相同的根。移动 不能跨卷工作。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    如果test 是一个目录,它将不起作用,正如documentation for Move-Item 所述:

    Move-Item 将在同一提供商支持的驱动器之间移动文件,但它只会在同一驱动器内移动目录。

    在这种情况下,您可以使用Copy-Item 后跟Remove-Item

    try {
      Copy-Item -Recurse C:\test \\192.168.1.50\c$\test2 -ErrorAction Stop
      Remove-Item -Recurse c:\test
    } catch {}
    

    如果您不依赖 PSDrive,另一种选择是简单地使用 xcopy 或 robocopy。

    【讨论】:

    • 他们可能会想在这些上使用-recurse
    • 谢谢,乔尔。没想到cpi 不会打扰复制目录的内容。我很少用 PS 做文件系统的事情。
    • 如果您使用 CI+RI,您必须确保在 CI 失败时不会删除项目(例如无法到达的目的地)。结合 Try/catch 指令使用“-ErrorAction Stop”
    • Ilyax:他指的是Copy-ItemRemove-Item的组合。
    • 我可以用 10 行 C# 来处理这个过程,但我想通过 powershell 来弥补这个过程是多么困难,文件夹中的文件夹和文件不能复制
    【解决方案2】:

    这需要加强,可能应该做成一个函数,但它可以工作。

    $source = "<UNC Path>"
    $destination = "<UNC Path>"
    
    if (test-path $destination -PathType Container) 
    {
      foreach ( $srcObj in (get-childitem $source )) 
      { 
        $srcObjPath = "$($srcObj.fullname)"
        $destObjPath = "$($destination)\$($srcObj.name)" 
        If((Test-Path -LiteralPath $destination)) 
         {
           copy-item $srcObjPath $destination -recurse
           if ( (Test-Path -Path $destObjPath ) -eq $true)
           {
            if ( (compare-object (gci $srcObjPath -recurse) (gci $destObjPath -recurse)) -eq $null)
            {
             write-output "Compare is good. Remove $($srcObjPath)"
             remove-item $srcObjPath -recurse
            }
            else
            {
              write-output "Compare is bad. Remove $($destObjPath)"
              remove-item $destObjPath -recurse
             }
           }
           else 
           { 
            write-output "$($destination) path is bad" 
           }
         }
      else
      {
        write-output "bad destinaton: $($destination)"
      }
     }
    }
    

    【讨论】:

      【解决方案3】:

      我有类似的问题。我发现在目标文件夹上,用户权限是有限的。我更改了源驱动器和目标驱动器的完全权限,并且移动顺利。 move-item 命令上没有相同的根错误消息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-28
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        相关资源
        最近更新 更多