Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

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
Saul1

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