Kalakutas
Kalakutas

Reputation: 144

Count number of uploaded file using PowerShell and WinSCP .NET assembly

I have script for upload files from my computer to FTP. But I wanna count how much files my script upload. Im using script by WinSCP.

Add-Type -Path "WinSCPnet.dll"

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.DebugLogPath = "C:\forKeppLog\transfer_data_script.log"
    $session.Open($sessionOptions)
    
    #Message
    Write-Host -ForegroundColor Cyan "`r`nUploading files to $servername..."
    Write-Host -ForegroundColor White "Do not close this window!"
    $path = "/MyuploadfilesTOFtp/"


    # Transfer files
    $session.PutFiles("C:\Myfileslocation\*.*", $path+"/*").Check()
}
finally
{
    #Inform user
    Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername."

    #Dispose of session
    $session.Dispose()
}

I can see what files was uploaded in my log file, but its not friendly for using. I wanna get some variable for counted files number who was uploaded.

I wanna make something like this

Write-Host -ForegroundColor Cyan "Files have been uploaded to $servername. Uploaded file's number is $numberofiles."

Upvotes: 1

Views: 781

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202721

The Session.PutFiles returns TransferOperationResult. It has Transfers property with a collection of the transfers.

$results = $session.PutFiles("C:\Myfileslocation\*.*", $path+"/*")
$results.Check() # Throws if any transfer failed

$count = $results.Transfers.Count

Upvotes: 2

Related Questions