locked
Download the file from SFTP and skip if file already download(or it was in Destination folder) RRS feed

  • Question

  • I know how to download the file from SFTP, But is there any simple solution to download the only files that were not downloaded previously??



    Tuesday, April 14, 2020 8:39 PM

All replies

  • That question is way too generic.

    Yes, you can probably automate/script this.

    Tuesday, April 14, 2020 8:44 PM
  • Compare them against a list of files already in the target directory? Get-ChildItem will get you that list.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, April 14, 2020 9:46 PM
  • Hi,

    Maybe you can try this :

       $session = New-Object WinSCP.Session
    
        try
        {
            # Connect
            $session.Open($sessionOptions)
    
            # Download files
            $transferOptions = New-Object WinSCP.TransferOptions
            $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
    
            $transferResult =
                $session.GetFiles("/home/user/*", "d:\download\*", $False, $transferOptions)
    
            # Throw on any error
            $transferResult.Check()
            
            #skip if file already download(Add download path at xxxxxxxx)
            if (Test-Path(xxxxxxxxxxxxxxxxxxx) )
    {
        Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow
        return
    }
    
            # Print results
            foreach ($transfer in $transferResult.Transfers)
            {
                Write-Host "Download of $($transfer.FileName) succeeded"
            }
        }
        finally
        {
            # Disconnect, clean up
            $session.Dispose()
        }
    
        exit 0
    }
    catch [Exception]
    {
        Write-Host "Error: $($_.Exception.Message)"
        exit 1
    }

    You can get more information by visiting the following link:

    https://winscp.net/eng/docs/library_session_getfiles#powershell

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-7

    Best wishes,

    Young Yang 


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, April 15, 2020 2:29 AM