2017-05-09 3 views
2

このスクリプトはPowerShellで完全に機能します。特定のタイプのすべてのファイルをコピーします。しかし、私はそのフォルダに&サブフォルダのファイルをコピーしたい。サブフォルダを含むフォルダをコピーする方法は?

$dest = "C:\example" 
$files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse 

foreach ($file in $files) { 
    $file_path = Join-Path -Path $dest -ChildPath $file.Name 

    $i = 1 

    while (Test-Path -Path $file_path) { 
     $i++ 
     $file_path = Join-Path -Path $dest -ChildPath 
     "$($file.BaseName)_$($i)$($file.Extension)" 
    } 

    Copy-Item -Path $file.FullName -Destination $file_path 
} 
+0

お持ちたいどんな機能の詳細をで説明してください。いくつかの実用的な例が良いスタートになるでしょう。 – vonPryz

+0

こんにちは、例えば、フォルダに1000個の.txtファイルがあります。これで正常にコピーされますが、そのファイルを含むフォルダはコピーされません。要するに、 –

+0

は、フォルダをコピーせず、ファイルをそこにコピーします。 –

答えて

14

パワーヒント:使用PowerShellは、フォルダ構造のアイテムをコピーして保持する

出典:https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/04/powertip-use-powershell-to-copy-items-and-retain-folder-structure/

質問:はどのように私はドライブからフォルダ構造をコピーするには、Windows PowerShellの3.0を使用することができます元の構造を保持していますか?

回答:Copy-Itemコマンドレットを使用して–Containerはスイッチパラメーターを指定します。

$sourceRoot = "C:\temp" 
$destinationRoot = "C:\n" 

Copy-Item -Path $sourceRoot -Filter "*.txt" -Recurse -Destination $destinationRoot -Container 
+0

私のコードを編集することができますか? –

+0

$ dest = "C:\ n" コピーアイテム - パス$ソースルート - フィルター "* .txt" - レスキュー - デスティネーション$ destinationRoot - コンテナー $ files = Get- ChildItemコマンド-Path "C:\管理サポート" -Filter "* .msgの" -Recurse のForEach($ $ファイルで提出){ $ FILE_PATH =参加パス-Path $ destの-ChildPath $ file.Name $ I = 1 (テストパス-Path $ FILE_PATH){ $ I ++ $ FILE_PATH =参加パス-Path $ DEST -ChildPath「$($ file.BaseName)_ $($ I)一方$($ file.Extensioそれは作業を行いn)が」 } コピー項目-Path $ file.FullName -Destination $ file_pathに } –

+0

ありません。私はそのラインメイトを追加しました。 –

関連する問題