2

How could I download the latest files, or files that were posted a certain amount of days? Importing a CSV file that contains a Source and a Destination column. Needs to check, if the path exists/file exists and only download new files.

Script right now is moving all the files to the correspondent folder - but once I run the script again, its not downloading only new files.

Here's an example of the CSV file:

try{

  Add-Type -Path "WinSCPnet.dll"
  # Setup session options
  $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::sftp
    HostName = "xxxxx"
    UserName = "xxxxr"
    Password = "xxxxxxx"
    PortNumber = "xx"
    GiveUpSecurityAndAcceptAnySshHostKey = $true
  }

  $session = New-Object WinSCP.Session

  try{
    # Connect
    $session.Open($sessionOptions)
    # Download files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

    Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach{

      Write-Host $_.Source
      Write-host $_.Destination 

      $transferResult =
        $session.GetFiles($_.Source, $_.Destination, $False, $transferOptions)

      # Throw on any error
      $transferResult.Check()

      # Print results
      $smtpBody = ""
      foreach ($transfer in $transferResult.Transfers){

        Write-Host "Download of $($transfer.FileName) succeeded"

        $smtpbody += " Files : ( $transfer.filename - join ', ')" +
                     " Current location: $($_.Destination) "
      } 

      Send-Mail Message @smtpMessage  -Body $smtpbody
    }
    finally {
      # Disconnect, clean up
      $session.Dispose()
    }
  }
  catch
  {
    Write-Host "Error: $($_.Exception.Message)"
  }
}

Example of the CSV file:

Source, Destination
/client1/Business/2019, C:\test\test1
/client2/Reporting/2018, C:\test\test2
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user 9191
  • 717
  • 4
  • 11
  • 31

1 Answers1

1

If your Source and Destination are plain directory paths, you can simply replace Session.GetFiles with Session.SynchronizeDirectories:

Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach {

    $synchronizationResult = $session.SynchronizeDirectories(
        [WinSCP.SynchronizationMode]::Local, $_.Destination, $_.Source, $False)

    $synchronizationResult.Check()

    foreach ($download in $synchronizationResult.Downloads)
    {
        Write-Host "File $($download.FileName) downloaded"
    }
}

See also WinSCP FAQ How do I transfer new/modified files only?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • You can also check the remote file modified date without downloading all files with [Session.ListDirectory](https://winscp.net/eng/docs/library_session_listdirectory#powershell_example) – Rich Moss Jan 14 '20 at 21:25
  • @RichMoss Yes. But that's what `Session.SynchronizeDirectories` does internally. And it does the download for you as well. So it is a way easier to use `Session.SynchronizeDirectories`, unless you need to customize the process in a way that is is not capable of. – Martin Prikryl Jan 15 '20 at 06:42