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

Henk50

Re: Not all synced files in $synchronizationResult.Uploads (powershell) if remote subdir not exist

You've done a great job! Problem solved, many thanks!!

I like WinSCP and the powershell library very much.
Henk50

Not all synced files in $synchronizationResult.Uploads (powershell) if remote subdir not exist

I am new to WinSCP, but found the following (bug?):
I have modified this script: Deleting remote files after successful remote to local synchronization
to do it the other way around(upload).
It is working OK IF the destination directories already exists, but if not, ONE file in each directory (except in the latest directory) is missing in $synchronizationResult.Uploads, so that file will not be deleted from source directory. The synchronisation was OK, all files done. What's wrong?
My test source directory is C:\...\Postbus UIT\Test1\Test2\Test3
files in Postbus UIT: Test0.txt; Thimo 2015 Postvak_LUIT - Copy.jpg; Thimo 2015 Postvak_LUIT.jpg
files in Test1: Test1.txt; Thimo 2015 Postvak_LUIT_TEST2 - Copy.jpg; Thimo 2015 Postvak_LUIT_TEST2.jpg
files in Test2: Test2.txt; Thimo 2015 Postvak_LUIT_TEST_TEST - Copy.jpg; Thimo 2015 Postvak_LUIT_TEST_TEST.jpg
files in Test3: Test3.txt; Thimo 2015 Postvak_LUIT_TEST_TEST - Copy.jpg; Thimo 2015 Postvak_LUIT_TEST_TEST.jpg
After running the script: Test0.txt, Test1.txt, Test2.txt still exist in source directory.

It looks to be, that if the sync operation have to create a remote-directory, one file is missing/skipped in $synchronizationResult.Uploads????
Running latest version 5.13.6.

Any idea?

Source:
param (
    $localPath = "C:\TEMP\WinSCP_Test\Data\Postvak UIT\",
    $remotePath = "/Postvak IN/",
    $LogFile_Session = "C:\TEMP\SessionLogX.log"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\TEMP\WinSCP_Test\WinSCPnet.dll"
   
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
 
        HostName = ""
        UserName = ""
        Password = ""
        SshHostKeyFingerprint = ""
    }
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = $LogFile_Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Synchronize files to local directory, collect results
        $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath, $false,[WinSCP.SynchronizationCriteria]::Time)
 
        # Deliberately not calling $synchronizationResult.Check
        # as that would abort our script on any error.
        # We will find any error in the loop below
        # (note that $synchronizationResult.Uploads is the only operation
        # collection of SynchronizationResult that can contain any items,
        # as we are not removing nor uploading anything)
 
        # Iterate over every Upload
        foreach ($Upload in $synchronizationResult.Uploads)
        {
            # Success or error?
            Write-Host "==== Upload of $($Upload.FileName) ==="
 
            if ($Upload.Error -eq $Null)
            {
                Write-Host "Download of $($Upload.FileName) succeeded, removing from source"
                # Download succeeded, remove file from source
                $filename = [WinSCP.RemotePath]::EscapeFileMask($Upload.FileName)
                Remove-Item($filename)
                if( -not $? )
                {
                    Write-Host "Removing of file $($Upload.FileName) failed"
                }
                else
                {
                    Write-Host "Removing of file $($Upload.FileName) succeeded"
                }
            }
            else
            {
                Write-Host (
                    "Upload of $($Upload.FileName) failed: $($Upload.Error.Message)")
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}