programing

하위 폴더로 폴더를 복사하는 방법은 무엇입니까?

codeshow 2023. 8. 26. 00:07
반응형

하위 폴더로 폴더를 복사하는 방법은 무엇입니까?

이 스크립트는 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
}

PowerTip: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-Itemcmdlet을 지정하고 다음을 지정합니다.–Container전환된 매개 변수:

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

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

언급URL : https://stackoverflow.com/questions/43867840/how-to-copy-folder-with-subfolders

반응형