簡體   English   中英

powershell獲取項目| 復制項目不只保留一個文件夾的文件夾結構

[英]powershell get-item | copy-item not keeping folder structure for just one folder

我試圖使用Powershell腳本將所有文件夾/子文件夾/子文件從服務器上的一個文件夾移動到另一個文件夾,同時始終保持相同的文件結構。 為此,我正在使用Get-childItem | 復制項目。 除目錄中的第一個文件夾外,所有文件夾,子文件夾和子文件都可以正常移動。 而是將所有這些子文件/子文件夾從此文件夾輸出到目標位置。 但是,它不會保留其結構,只是不包括其父目錄。 我的代碼如下:

Get-ChildItem $sourceFilePath -Force | Copy-Item -Destination ("*file path*" -f $destinationServer, $destinationClient) -Recurse -Force 
  • 改寫文件路徑以提高可讀性
  • $ sourceFilePath是我要復制的源文件夾
  • $ destination服務器/ $ destinationClient是在釋義的“文件路徑”中使用的變量

我無法弄清楚為什么此代碼除了適用於該單個文件夾及其項目以外,還適用於所有其他文件夾,子文件夾和文件。 我們將不勝感激,如果有任何其他信息可以幫助您,請告訴我。


感謝@ChristianMüller:

New-Item $destinationFilePath -name "MissingFolder" -type directory -Force | Out-Null

Get-ChildItem $sourceFilePath -Force | Copy-Item -Destination ("*filepath*" -f $destinationServer, $destinationClient) -Recurse -Force

這是一個非常奇怪的錯誤,也令我驚訝! :)

似乎目標基本目錄不存在,而是僅創建目錄而不包含內容。

因此,通過這種構造,您甚至可以在ISE中調試此行為:

$sourceFilePath = "c:\temp\Test1"
$destPath = "c:\temp\Test2"
$a=Get-ChildItem $sourceFilePath -Force

rm -Force $destPath
foreach($x in $a) {
    $x | Copy-Item -Destination "$destPath" -Recurse -Force 
}

dir $destPath

首先使用新項目創建目標目錄,以解決該問題:

$sourceFilePath = "c:\temp\Test1"
$destPath = "c:\temp\Test2"
$a=Get-ChildItem $sourceFilePath -Force

rm -Force $destPath

New-Item -ItemType Directory -Force -Path $destPath
foreach($x in $a) {
    $x | Copy-Item -Destination "$destPath" -Recurse -Force 
}

dir $destPath

但以我的示例為例,完全不使用“ Get-ChildItem”即可

Copy-Item c:\temp\test1 -Destination c:\temp\test2 -Recurse -Force

這對您也有用嗎?

Copy-Item $sourceFilePath -Destination ("*file path*" -f $destinationServer, $destinationClient) -Recurse -Force 

暫無
暫無

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

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