Post a reply

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)


Topic review


Re: Delete after Syncing Causing Error (SynchronizeDirectories)

What I ended up doing is changing the conditional statement:
if ($sourceLength -eq $destLength -and $destLength -ne 0)

This resolved my issue...keeping my fingers crossed!

Re: Delete after Syncing Causing Error (SynchronizeDirectories)

This is a common problem, for which there's no generic solution. You have to build a custom solution using options that your system allows.

Delete after Syncing Causing Error (SynchronizeDirectories)

I'm using SynchronizeDirectories to copy files from one server to another. After copying, I'm deleting the files from the source using the "Deleting remote files after successful remote to local synchronization" solution (
Files are available at the end of every month OR they can be generated adhoc. The requirement is that the files be available at the destination, which is a file server, as soon as possible so that users can access them. So I have it scheduled to sync every minute.
The problem I'm having is the first file of each month is very large so while the file is being generated, the process is downloading and then deleting the file before it's completed. This generates an error in the application generating the file as well as a 0 byte file.
I changed the script to compare the size of the file at source with the size of the file at destination before the deleting. If they aren't equal don't delete. Otherwise delete it. I also changed the sync criteria to size so that if the file isn't the same size, the next time the process kicks off, it will check the size and then sync the remainder of the file. That doesn't work because the first file ends up being equal: 0 bytes.
I then added that when after the sync occurs then it sleeps for 20 seconds before performing the delete. This has worked so far with the monthly files but when the year end files were generated as part of a test, the error occurred again.
I'm not sure if there are other options for these type of scenario or a better way of doing this...we're just sooo close with this!
The code is as follows:
param ($Source, $Destination, $Protocol, $HostName, $UserName, $Password, $FingerPrint, $SessionLog)

# Load WinSCP .NET assembly
Add-Type -Path "E:\Program Files\WinSCP 5.13\WinSCPnet.dll"
# Main script
   $session = New-Object WinSCP.Session
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::$Protocol
        HostName = "$($HostName)"
        UserName = "$($UserName)"
        Password = "$($Password)"
      SshHostKeyFingerprint = $FingerPrint 
      if(!(Test-Path -Path $Destination )){
         New-Item -ItemType directory -Path $Destination
       $session.SessionLogPath = "$($SessionLog)"
        Write-Host "Connected... " (Get-Date).ToString()
      $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
        Write-Host "Synchronize files... " (Get-Date).ToString()
         $synchronizationResult = $session.SynchronizeDirectories(
            [WinSCP.SynchronizationMode]::Local, "$($Destination)", "$($Source)", $False, $False, [WinSCP.SynchronizationCriteria]::Size, $transferOptions)
      Write-Host "Sleeping for 20... " (Get-Date).ToString()
      Start-Sleep -s 20
      Write-Host "Continuing processing... " (Get-Date).ToString()
      foreach ($download in $synchronizationResult.Downloads)
         # Success or error?
            if ($download.Error -eq $Null){
                Write-Host "Download of $($download.FileName) succeeded, removing from source " (Get-Date).ToString()
                # Download succeeded, remove file from source. This includes the full path
                $fileNameSource = [WinSCP.RemotePath]::EscapeFileMask($download.FileName)
            $fileNameDestArray = $download.FileName -split "/"
            $sourceLength = $session.GetFileInfo($fileNameSource).Length
            Write-Host "Source file size " $sourceLength
            $fileNameDest = $Destination + "\" + $fileNameDestArray[6]+ "\"+ $fileNameDestArray[7]+ "\" + $fileNameDestArray[8]
            $destLength = (Get-Item $fileNameDest).Length
            Write-Host "Destination file size" $destLength
            if ($sourceLength -eq $destLength){
               Write-Host "Attempting to remove $($download.FileName) "  (Get-Date).ToString()
               $removalResult = $session.RemoveFiles($fileNameSource)
               if ($removalResult.IsSuccess){
                  Write-Host "Removing of file $($download.FileName) succeeded "  (Get-Date).ToString()
                  Write-Host "Removing of file $($download.FileName) failed"  (Get-Date).ToString()
                  Write-Host "File $($download.FileName) was not removed. File size of downloaded file is not equal. File is still being written to Oracle."  + (Get-Date).ToString()
            Write-Host "Download of $($download.FileName) failed: $($download.Error.Message)"   (Get-Date).ToString()
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1

Let me know. Thanks!