Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I amended the code from stej (which was great, just what i needed!) to use larger buffer, [long] for larger files and used System.Diagnostics.Stopwatch class to track elapsed time and estimate time remaining.</p> <p>Also added reporting of transfer rate during transfer and outputting overall elapsed time and overall transfer rate.</p> <p>Using 4MB (4096*1024 bytes) buffer to get better than Win7 native throughput copying from NAS to USB stick on laptop over wifi.</p> <p>On To-Do list:</p> <ul> <li>add error handling (catch) </li> <li>handle get-childitem file list as input</li> <li>nested progress bars when copying multiple files (file x of y, % if total data copied etc)</li> <li>input parameter for buffer size</li> </ul> <p>Feel free to use/improve :-)</p> <pre><code>function Copy-File { param( [string]$from, [string]$to) $ffile = [io.file]::OpenRead($from) $tofile = [io.file]::OpenWrite($to) Write-Progress ` -Activity "Copying file" ` -status ($from.Split("\")|select -last 1) ` -PercentComplete 0 try { $sw = [System.Diagnostics.Stopwatch]::StartNew(); [byte[]]$buff = new-object byte[] (4096*1024) [long]$total = [long]$count = 0 do { $count = $ffile.Read($buff, 0, $buff.Length) $tofile.Write($buff, 0, $count) $total += $count [int]$pctcomp = ([int]($total/$ffile.Length* 100)); [int]$secselapsed = [int]($sw.elapsedmilliseconds.ToString())/1000; if ( $secselapsed -ne 0 ) { [single]$xferrate = (($total/$secselapsed)/1mb); } else { [single]$xferrate = 0.0 } if ($total % 1mb -eq 0) { if($pctcomp -gt 0)` {[int]$secsleft = ((($secselapsed/$pctcomp)* 100)-$secselapsed); } else { [int]$secsleft = 0}; Write-Progress ` -Activity ($pctcomp.ToString() + "% Copying file @ " + "{0:n2}" -f $xferrate + " MB/s")` -status ($from.Split("\")|select -last 1) ` -PercentComplete $pctcomp ` -SecondsRemaining $secsleft; } } while ($count -gt 0) $sw.Stop(); $sw.Reset(); } finally { write-host (($from.Split("\")|select -last 1) + ` " copied in " + $secselapsed + " seconds at " + ` "{0:n2}" -f [int](($ffile.length/$secselapsed)/1mb) + " MB/s."); $ffile.Close(); $tofile.Close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload