2016-06-14 15 views
0

新しいファイルをサーバー上の共有フォルダにコピーするPowerShellスクリプトを作成しました。PowerShell - 複数のファイルをコピーする

サブフォルダ内の新しいファイルのリストを取得した後で、for-eachを使用して一度に1つずつコピーする以外に、サブフォルダをコピーすることができますか?進行状況バーを追加することができます。サイズにかかわらず、全ファイルの進行状況が表示されます:このような

+1

をしかし、それはforeachのオブジェクトでプログレスバーを提供する方が簡単です。 '$ファイル| foreach {Write-Progress -Activity "コピー" -PercentComplete(($ index ++)/ $ files.count)* 100;コピー$ _ "\\ server \ destination"} – TessellatingHeckler

+0

ありがとうTessellatingHeckler Windowsエクスプローラを経由してコピーし、そのようにプログレスバーを取得するには、以前の検索として機能するかどうかはわかりませんでした。これは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 

NOTE出発点である可能性があります。例:2つのファイルがあり、1つは1 MB、もう1つは50 MBです。最初のファイルがコピーされると、ファイルの半分がコピーされるため、進行状況は50%になります。もしあなたが合計バイトで進歩したいなら、この機能を試してみることを強くお勧めします。ソースと目的地を与えてください。

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

スクリーンショットをコピーするために、単一のファイルまたはフォルダ全体を与えられたときに動作します:http://i.imgur.com/hT8yoUm.jpg

+0

ありがとうアンソニー 病気を見てみましょう – Grantson

関連する問題