Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I show the file download process – POWERSHELL

I have a script that downloads some recordings from an ftp server and the downloads are saved in a folder inside a local path on my computer.

I have been able to trigger a message when all the recordings have been downloaded successfully, but I wanted instead to display a message every time each of the ftp recordings are downloaded. How can I do that?

**this is the script **

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"


$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ip of ftp"
    PortNumber = port number
    UserName = "user"
    Password = "credentials"
}

$session = New-Object WinSCP.Session

try
{
   
    $session.Open($sessionOptions)

    
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult = $session.GetFiles($remotePath, $localPath, $False, $transferOptions)
 
        $transferResult.Check()

        

        foreach ($transfer in $transferResult.Transfers)
        {
          Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
        }

        
    
}
finally
{
    $session.Dispose()
}

I have looked for ways to do it but I can’t find the answer.

>Solution :

If you want to show just the names of the files as they finish transferring, use Session.FileTransferred event:

function FileTransferred
{
    param($e)
 
    if ($e.Error -eq $Null)
    {
        Write-Host "Download of $($e.FileName) succeeded"
    }
    else
    {
        Write-Host "Download of $($e.FileName) failed: $($e.Error)"
    }
}

$session.add_FileTransferred( { FileTransferred($_) } )

$session.Open($sessionOptions)

...

$session.GetFiles($remotePath, $localPath, $False, $transferOptions).Check()

If you want to display progress of individual file transfers, you can use Session.FileTransferProgress.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading