Differences

This shows you the differences between the selected revisions of the page.

library_example_keep_local_directory_up_to_date 2020-07-15 library_example_keep_local_directory_up_to_date 2020-07-15
Line 25: Line 25:
# @description  Periodically scans for changes in a remote directory and ^ # @description  Periodically scans for changes in a remote directory and ^
#                  reflects them on a local directory #                  reflects them on a local directory
-# @version      6+# @version      7
# @homepage    ~~SELF~~ # @homepage    ~~SELF~~
# @require      WinSCP 5.16 # @require      WinSCP 5.16
Line 60: Line 60:
    $pause     $pause
) )
 +
 +function Beep()
 +{
 +    if ($beep)
 +    {
 +        [System.Console]::Beep()
 +    }
 +}
 +
 +function HandleException ($e)
 +{
 +    if ($continueOnError)
 +    {
 +        Write-Host -ForegroundColor Red $_.Exception.Message
 +        Beep
 +    }
 +    else
 +    {
 +        throw $e
 +    }
 +}
try try
Line 73: Line 94:
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
       
-    # Optimization 
-    # (do not waste time enumerating files, if you do not need to scan for deleted files) 
-    if ($delete)  
-    { 
-        $localFiles = Get-ChildItem -Recurse -Path $localPath 
-    } 
- 
    try     try
    {     {
Line 89: Line 103:
        while ($True)         while ($True)
        {         {
-            Write-Host "Synchronizing changes..." +            Write-Host -NoNewline "Looking for changes..." 
-            $result+            try 
- ···············$session.SynchronizeDirectories+            { 
- ···················[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $delete)+················$differences
 + ···················$session.CompareDirectories
 + ·······················[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $delete)
-············$changed = $False +················Write-Host 
- + ···············if ($differences.Count -eq 0)
- ···········if (!$result.IsSuccess) +
- ···········+
-               if ($continueOnError)+
                {                 {
-                    Write-Host "Error: $($result.Failures[0].Message)" +                    Write-Host "No changes found." ··
-                   $changed = $True+
                }                 }
                else                 else
                {                 {
-                    $result.Check() +                    Write-Host "Synchronizing $($differences.Count) change(s)..."; 
-················} + ···················Beep
- ···········}+
-············# Print updated files +····················foreach ($difference in $differences) 
- ···········foreach ($download in $result.Downloads+ ···················
- ···········+ ·······················$action = $difference.Action 
- ···············Write-Host "$($download.Destination)·<= $($download.FileName)" +                        if ($action -eq [WinSCP.SynchronizationAction]::DownloadNew
- ···············$changed = $True + ·······················
- ···········}+ ···························$message = "Downloading new $($difference.Remote.FileName)..." 
 +                        } 
 +                        elseif ($action -eq [WinSCP.SynchronizationAction]::DownloadUpdate) 
 +                        { 
 +                            $message = "Downloading updated $($difference.Remote.FileName)...
 +                       } 
 +                        elseif ($action -eq [WinSCP.SynchronizationAction]::DeleteLocal) 
 +                        { 
 +                            $message = "Deleting $($difference.Local.FileName)..." 
 +                        } 
 +                        else 
 +                        { 
 +                            throw "Unexpected difference $action" 
 + ·······················}
-············if ($delete) +························Write-Host -NoNewline $message
-            { +
-                # scan for removed local files (the $result does not include them) +
-                $localFiles2 = Get-ChildItem -Recurse -Path $localPath+
-················if ($localFiles) +························try 
- ···············+ ·······················
- ···················$changes = + ···························$difference.Resolve($session) | Out-Null 
-························Compare-Object -DifferenceObject $localFiles2 ` + ···························Write-Host " Done.
-                            -ReferenceObject $localFiles +                        } 
-                 + ·······················catch 
-                    $removedFiles = + ·······················
-                        $changes | + ···························Write-Host 
-                       Where-Object -FilterScript { $_.SideIndicator -eq "<=" } | + ···························HandleException $_ 
-                        Select-Object -ExpandProperty InputObject +                        }
- +
- ···················# Print removed local files +
-                   foreach ($removedFile in $removedFiles) +
-····················+
- ·······················Write-Host "$removedFile deleted" +
-                        $changed = $True+
                    }                     }
                }                 }
- 
-                $localFiles = $localFiles2 
            }             }
- +············catch
-············if ($changed)+
            {             {
-                if ($beep) +                Write-Host 
-                +                HandleException $_
-                   [System.Console]::Beep() +
-················}+
            }             }
-            else +
-            { +
-                Write-Host "No change." +
-            } +
-············+
            Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."             Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."
            $wait = [int]$interval             $wait = [int]$interval
Line 176: Line 182:
catch catch
{ {
-    Write-Host &quot;Error: $($_.Exception.Message)"+    $continueOnError = $True 
 +····HandleException $_
} }

Last modified: by martin