PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How to download newer files using WinSCP and save them to two local folders?

 August 01, 2022     batch-file, cmd, ftp, winscp     No comments   

Issue

I have file in a folder (ftp_region) whose contents is similar to data in my FTP.

How to download newer files from my FTP and save them to two local folders (ftp_region and other folder)

For now, I use this:

open "my ftp address"
get -neweronly "..." "..."

Solution

You cannot do this with a plain WinSCP scripting.

You better use the WinSCP .NET assembly. It allows you to process the downloaded files.

An example code in PowerShell:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "password"
}

Write-Host "Connecting..."
$session = New-Object WinSCP.Session
# $session.SessionLogPath = "C:\path\to\log\sync.log"
$session.Open($sessionOptions)

$remotePath = "/remote/path"
$localPathPrimary = "C:\backup\primary"
$localPathSecondary = "C:\backup\secondary"

Write-Host "Synchronizing..."
$result =
    $session.SynchronizeDirectories(
        [WinSCP.SynchronizationMode]::Local, $localPathPrimary, $remotePath,
        $False)
$result.Check()

Write-Host "Copying downloaded files to secondary backup..."
foreach ($download in $result.Downloads)
{
    $filename = (Split-Path -Leaf $download.Destination)
    $localFilePathSecondary = (Join-Path $localPathSecondary $filename)
    Copy-Item $download.Destination $localFilePathSecondary
    Write-Host "File $filename archived to both folders."
}

The code is based on an official example Deleting remote files after successful remote to local synchronization.



Answered By - Martin Prikryl
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing