2016-06-14 2 views
0

새 파일을 서버의 공유 폴더로 복사하는 PowerShell 스크립트를 작성했습니다.PowerShell - 여러 파일 복사 중

하위 폴더에 새 파일 목록을 얻은 후에도 for-each를 사용하고 한 번에 하나씩 복사하는 대신 다른 파일을 함께 복사 할 수 있는지 궁금합니다. 진행률 표시 줄을 추가 할 수 있습니다. 크기에 관계없이 전체 파일에 대한 진행 상황을 보여줍니다 :이 같은

+1

을하지만 foreach는 객체와 진행률 표시 줄을 제공하는 것이 더 쉽습니다; '$ files | foreach {쓰기 진행 - 활동 "복사"-PercentComplete (($ index ++)/$ files.count) * 100; 복사 $ _ "\\ server \ destination"} ' – TessellatingHeckler

+0

고맙습니다. TessellatingHeckler Windows 탐색기를 통해 복사하고 진행률 막대를 그 방법으로 얻으려면 .Net copyhere 메서드를 사용하여 이전 검색으로 작동하는지 확실하지 않았습니다. 현재 foreach 루프 내부에서 각 파일의 진행률 막대를 표시하는 순간에 수행중인 작업입니다. 아픈 시도해주세요 – Grantson

답변

0

뭔가 출발점을

# define source and destination folders 
$source = 'C:\temp\music' 
$dest = 'C:\temp\new' 

# get all files in source (not empty directories) 
$files = Get-ChildItem $source -Recurse -File 

$index = 0 
$total = $files.Count 
$starttime = $lasttime = Get-Date 
$results = $files | % { 
    $index++ 
    $currtime = (Get-Date) - $starttime 
    $avg = $currtime.TotalSeconds/$index 
    $last = ((Get-Date) - $lasttime).TotalSeconds 
    $left = $total - $index 
    $WrPrgParam = @{ 
     Activity = (
      "Copying files $(Get-Date -f s)", 
      "Total: $($currtime -replace '\..*')", 
      "Avg: $('{0:N2}' -f $avg)", 
      "Last: $('{0:N2}' -f $last)", 
      "ETA: $('{0:N2}' -f ($avg * $left/60))", 
      "min ($([string](Get-Date).AddSeconds($avg*$left) -replace '^.* '))" 
     ) -join ' ' 
     Status = "$index of $total ($left left) [$('{0:N2}' -f ($index/$total * 100))%]" 
     CurrentOperation = "File: $_" 
     PercentComplete = ($index/$total)*100 
    } 
    Write-Progress @WrPrgParam 
    $lasttime = Get-Date 

    # build destination path for this file 
    $destdir = Join-Path $dest $($(Split-Path $_.fullname) -replace [regex]::Escape($source)) 

    # if it doesn't exist, create it 
    if (!(Test-Path $destdir)) { 
     $null = md $destdir 
    } 

    # if the file.txt already exists, rename it to file-1.txt and so on 
    $num = 1 
    $base = $_.basename 
    $ext = $_.extension 
    $newname = Join-Path $destdir "$base$ext" 
    while (Test-Path $newname) { 
     $newname = Join-Path $destdir "$base-$num$ext" 
     $num++ 
    } 

    # log the source and destination files to the $results variable 
    Write-Output $([pscustomobject]@{ 
     SourceFile = $_.fullname 
     DestFile = $newname 
    }) 

    # finally, copy the file to its new location 
    copy $_.fullname $newname 
} 

# export a list of source files 
$results | Export-Csv c:\temp\copylog.csv -NoTypeInformation 

참고가 될 수 있습니다. 예 : 파일이 2 개 있는데, 하나는 1MB이고 다른 하나는 50MB입니다. 첫 번째 파일을 복사 할 때 파일의 절반이 복사되므로 진행률은 50 %입니다. 총 바이트 수를 늘리려면이 함수를 사용하는 것이 좋습니다. 그냥 소스와 목적지를 제공하십시오.

https://github.com/gangstanthony/PowerShell/blob/master/Copy-File.ps1

스크린 샷을 복사하는 하나의 파일 또는 전체 폴더를 투여했을 때 작동합니다 http://i.imgur.com/hT8yoUm.jpg

+0

감사합니다 안토니 아픈 기능을 살펴 봅니다. – Grantson