Synchronize to another remote directory

Advertisement

Saul1
Joined:
Posts:
13
Location:
Germany

Synchronize to another remote directory

Hello,
is it possible to do synchronize local-folder1 remote-folder2 -preview and copy the files not in local-folder1 into the remote-folder3?

Thanks
Wolfgang

Reply with quote E-mail

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,378
Location:
Prague, Czechia

Re: Syncronize

No.

Though once "preview" is implemented in .NET assembly, it would not be difficult to script this in PowerShell:
Issue 885 – Synchronization preview in scripting

But if you want to compare only files in folders (no subfolders), it's actually not difficult to implement even now.
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "example.com"
    UserName = "username"
    Password = "password"
    SshHostKeyFingerprint = "..."
}
 
$session = New-Object WinSCP.Session
 
# Connect
$session.Open($sessionOptions)
 
$localPath = "C:\local\source\path"
$remotePath1 = "/remote/reference/path"
$remotePath2 = "/remote/target/path"
 
$remoteFiles = $session.ListDirectory($remotePath1).Files | Where-Object { -Not $_.IsDirectory } | Select-Object -ExpandProperty Name
$localFiles = Get-ChildItem $localPath | Where-Object { -Not $_.PSIsContainer } | Select-Object -ExpandProperty Name
$changes = Compare-Object -DifferenceObject $localFiles -ReferenceObject $remoteFiles
 
$added =
    $changes |
    Where-Object -FilterScript { $_.SideIndicator -eq "=>" } |
    Select-Object -ExpandProperty InputObject
 
foreach ($filename in $added)
{
    Write-Host "Uploading $filename ..."
    $localFilePath = Join-Path $localPath $filename
    $remoteFilePath = [WinSCP.RemotePath]::CombinePaths($remotePath2, $filename)
    $session.PutFiles($localFilePath, $remoteFilePath).Check()
}

WinSCP GUI can generate the $sessionOptions section for you:
https://winscp.net/eng/docs/ui_generateurl#script

The code is partly based on these examples:
https://winscp.net/eng/docs/library_example_keep_local_directory_up_to_date
https://winscp.net/eng/docs/library_example_watch_for_changes

Reply with quote

Advertisement

You can post new topics in this forum