簡體   English   中英

如何使用 Powershell 跨卷移動文件/文件夾?

[英]How do you move files/folders across volumes with Powershell?

我嘗試使用 PowerShell 移動文件夾

move-item  c:\test c:\test2

有效,但是

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

不告訴我

移動項:源路徑和目標路徑必須具有相同的根。 移動不適用於卷。

如果test是一個目錄,它將不起作用,正如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 {}

如果您不依賴 PSDrives,另一種選擇是簡單地使用 xcopy 或 robocopy。

這需要收緊,可能應該做成 function 但它可以工作。

$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)"
  }
 }
}

我有類似的問題。 我發現在目標文件夾上,用戶權限是有限的。 我更改了源驅動器和目標驅動器的完全權限,並且移動順利。 move-item 命令上沒有相同的根錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM