Write Host on Removal of Files during Synchronize with RemoveFiles enabled – PowerShell

Advertisement

johnhamborg
Joined:
Posts:
5
Location:
Earth

Write Host on Removal of Files during Synchronize with RemoveFiles enabled – PowerShell

I am largely following the template provided here
https://winscp.net/eng/docs/library_session_synchronizedirectories#example

I have configured the Removal of Files during Synchronize with removeFiles enabled and I need to write out the status of those operations along with the other sync operations.

In the example, all errors and status are sent through the FilesTransferred function, how do I include the removed files as well?

I tried to add this $e.Removal case, but it does not seem to work.

if ($e.Touch -ne $Null)
{
    if ($e.Touch.Error -eq $Null)
    {
        Write-Host "Timestamp of $($e.Touch.FileName) set to $($e.Touch.LastWriteTime)"
    }
    else
    {
        Write-Host "Setting timestamp of $($e.Touch.FileName) failed: $($e.Touch.Error)"
    }
}
 
 
if ($e.Removal -ne $Null)
{
    if ($e.Removal.Error -eq $Null)
    {
        Write-Host "Deleted $($e.Removal.FileName) "
    }
    else
    {
        Write-Host "Could not Delete $($e.Removal.FileName): $($e.Removal.Error)"
    }
 
}
else
{
    # This should never happen during "local to remote" synchronization
    Write-Host "Timestamp of $($e.Destination) kept with its default (current time)"
}
Thanks

Reply with quote

Advertisement

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

Re: Write Host on Removal of Files during Synchronize with RemoveFiles enabled - PowerShell

There's no event for file removals.

But all synchronization operations on remote files are recorded in SynchronizationResult returned by Session.SynchronizeDirectories method.
https://winscp.net/eng/docs/library_synchronizationresult

Particularly, see the Removals property:
https://winscp.net/eng/docs/library_synchronizationresult#removals

Reply with quote

johnhamborg
Joined:
Posts:
5
Location:
Earth

Solved

Thanks for the pointer.

I ended up implementing it like this
$synchronizationResult =
    $session.SynchronizeDirectories(
        [WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath,
        $True, $False, [WinSCP.SynchronizationCriteria]::Time,
        $transferOptionsSync)
 
# Removals
$Removals = $synchronizationResult.Removals
ForEach ($file in $Removals) {
    Write-Host "Deleted " $File.FileName
}

Reply with quote

Advertisement

You can post new topics in this forum