简体   繁体   English

在 PowerShell 中使用 WinSCP 仅下载新文件

[英]Download only new files with WinSCP in PowerShell

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.导入包含SourceDestination列的 CSV 文件。 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:以下是 CSV 文件的示例:

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: CSV 文件示例:

Source, Destination
/client1/Business/2019, C:\test\test1
/client2/Reporting/2018, C:\test\test2

If your Source and Destination are plain directory paths, you can simply replace Session.GetFiles with Session.SynchronizeDirectories :如果您的SourceDestination是普通目录路径,您可以简单地将Session.GetFiles替换为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?另请参阅 WinSCP 常见问题解答如何仅传输新的/修改过的文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET WinSCP,仅下载新文件 - .NET WinSCP, only download new files 仅当文件存在时,才在 PowerShell 中使用 WinSCP 从 SFTP 服务器从阵列下载文件 - Download files from an array from SFTP server using WinSCP in PowerShell only when the files exist 仅使用 PowerShell 将新文件从 FTP 目录下载到本地文件夹 - Only download new files from FTP directory to local folder using PowerShell 使用 PowerShell 中的 WinSCP 将整个驱动器上传到服务器,跳过所有无法读取的文件和文件夹 - Upload whole drive to a server using WinSCP in PowerShell skipping all files and folders that cannot be read 使用 WinSCP .NET 程序集从 FTP 服务器下载除特定文件夹之外的所有文件 - Download all files except a specific folder from FTP server using WinSCP .NET assembly PowerShell 中的 WinSCP .NET 程序集 - 创建 SessionOptions - 提供的值无效,或者属性为只读 - WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only 使用 PowerShell 从 SFTP 服务器下载文件 - Download files from SFTP server using PowerShell 识别新同步的文件 (WinSCP) - Recognize newly synchronized files (WinSCP) PowerShell脚本未创建WinSCP会话的日志文件 - PowerShell script not creating log file of WinSCP session 使用 WinSCP 和 PowerShell 在文件上传之间暂停 - Pause between file uploads with WinSCP and PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM