When reporting transfer progress, access overall & current file size?

Advertisement

a.r
Joined:
Posts:
3
Location:
Germany

When reporting transfer progress, access overall & current file size?

Session.FileTransferProgress's event offers FileProgress and OverallProgress, but is there a way to also retrieve FileSize and OverallSize?

Getting both sizes via the event would be especially useful when using the convenience functions {Get,Put}FilesToDirectory that don't require one's code to individually reference files to be transferred.

My goal is to have both a graphical (overall) progress bar and a "[ $total_transferred_size / $total_overall_size ] $current_filename ]" status line underneath. The former is trivial, but the latter is a mess.

For PowerShell, I've tried looping through the individual files while using…
  • for uploads:
    (Get-Item $file.FullName).Length
  • for downloads:
    $files = $session.EnumerateRemoteFiles
    followed by $file.Length
… and then summing these for $total_overall_size and keeping track of $total_transferred_size during the individual transfers, but that doesn't quite seem to work.

It's very cumbersome compared to just being able to use the convenience functions, especially given the required sizes already have to be known to WinSCP internally (otherwise, how could it calculate the progresses).

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
42,204
Location:
Prague, Czechia

Re: When reporting transfer progress, access overall & current file size?

a.r wrote:

but that doesn't quite seem to work.
Why it doesn't work? What's the problem?

Reply with quote

a.r
Joined:
Posts:
3
Location:
Germany

Re: When reporting transfer progress, access overall & current file size?

I've now got it to work with (example snippets)…
…
    foreach ($file in $remoteFiles) {
        $Script:sizeFile = $file.Length
        $session.GetFiles("$file.Name", "$localPath", $false, $transferOptions).Check()
        $Script:sizeTransferred += $file.Length
    }
…
 
function FileTransferProgress {
    param($e)
    $transferredSizeLocal = $e.FileProgress * $Script:sizeFile + $Script:sizeTransferred
    $pgBar.Value = [int](100 * $e.OverallProgress)
    $pgBar.OverlayText = ("[{0:N0}/{1:N2} MB] {2}" -f ($transferredSizeLocal / 1MB), ($Script:sizeTotal / 1MB), (Split-Path -Path $e.FileName -Leaf))
    $pgBar.Refresh()
    [System.Windows.Forms.Application]::DoEvents()
}
… but again it's annoying to bend over backwards with two dozen extra lines of code (more if this wasn't such a simple use case), which you've got to come up with & maintain, and that c/should be entirely superfluous since WinSCP already has that very info internally.

I can't even seem to print $transferredSizeLocal with decimal places since $e.FileProgress * $Script:sizeFile seems to cast the variable into an integer.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
42,204
Location:
Prague, Czechia

Re: When reporting transfer progress, access overall & current file size?

Thanks for sharing your code.

Anyway, I have added this request to the tracker:
Issue 2390 – Include file and overall sizes into FileTransferProgressEventArgs
You can vote for it there.

To print $transferredSizeLocal / 1MB with decimal places, just replace N0 with number of decimal places you want to show, e.g. N2.

Reply with quote

Advertisement

You can post new topics in this forum