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 2023-06-21 (current)
Line 12: Line 12:
<code> <code>
-powershell.exe -File KeepLocalUpToDate.ps1 -sessionUrl "sftp://username:password@example.com/" -remotePath "/remote/path" -localPath "C:\local\path" [-delete]+powershell.exe -File KeepLocalUpToDate.ps1 -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -remotePath "/remote/path" -localPath "C:\local\path" [-delete]
</code> </code>
-//If you just want to scan for changes, but not download them, see [[library_example_watch_for_changes|Watching for changes in SFTP/FTP server]].//+//If you just want to scan for changes, but not download them, see [[library_example_watch_for_changes|*]].//
<code powershell - KeepLocalUpToDate.ps1> <code powershell - KeepLocalUpToDate.ps1>
Line 21: Line 21:
# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^ # @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^
#                  -sessionUrl "!E" -localPath "%LocalPath%" -remotePath "%RemotePath%" ^ #                  -sessionUrl "!E" -localPath "%LocalPath%" -remotePath "%RemotePath%" ^
-#                  %Delete% %Beep% %ContinueOnError% -interval "%Interval%" -pause+#                  %Delete% %Beep% %ContinueOnError% -interval "%Interval%" ^ 
-#                  -sessionLogPath "%SessionLogPath%"+#                  -fileMask "%FileMask%" -pause -sessionLogPath "%SessionLogPath%"
# @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      7+# @version      11
# @homepage    ~~SELF~~ # @homepage    ~~SELF~~
# @require      WinSCP 5.16 # @require      WinSCP 5.16
Line 37: Line 37:
# @option        ContinueOnError -config -run checkbox "Continue on &error" "" -continueOnError # @option        ContinueOnError -config -run checkbox "Continue on &error" "" -continueOnError
# @option        Interval -config -run textbox "&Interval (in seconds):" "30" # @option        Interval -config -run textbox "&Interval (in seconds):" "30"
 +# @option        FileMask -config -run textbox "File &mask:" ""
# @option      - -config group "Logging" # @option      - -config group "Logging"
# @option        SessionLogPath -config sessionlogfile # @option        SessionLogPath -config sessionlogfile
Line 44: Line 45:
param ( param (
    # Use Generate Session URL function to obtain a value for -sessionUrl parameter.     # Use Generate Session URL function to obtain a value for -sessionUrl parameter.
-    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...=@example.com/",+    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/",
    [Parameter(Mandatory = $True)]     [Parameter(Mandatory = $True)]
    $localPath,     $localPath,
Line 57: Line 58:
    $sessionLogPath = $Null,     $sessionLogPath = $Null,
    $interval = 30,     $interval = 30,
 +    $fileMask = $Null,
    [Switch]     [Switch]
    $pause     $pause
Line 80: Line 82:
        throw $e         throw $e
    }     }
 +}
 +
 +function SetConsoleTitle ($status)
 +{
 +    if ($sessionOptions)
 +    {
 +        $status = "$sessionOptions - $status"
 +    }
 +    $Host.UI.RawUI.WindowTitle = $status
} }
Line 91: Line 102:
    $sessionOptions = New-Object WinSCP.SessionOptions     $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.ParseUrl($sessionUrl)     $sessionOptions.ParseUrl($sessionUrl)
 +
 +    $transferOptions = New-Object WinSCP.TransferOptions -Property @{ FileMask = $fileMask };
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
Line 99: Line 112:
        Write-Host "Connecting..."         Write-Host "Connecting..."
 +        SetConsoleTitle "Connecting"
        $session.Open($sessionOptions)         $session.Open($sessionOptions)
Line 104: Line 118:
        {         {
            Write-Host -NoNewline "Looking for changes..."             Write-Host -NoNewline "Looking for changes..."
 +            SetConsoleTitle "Looking for changes"
            try             try
            {             {
                $differences =                 $differences =
                    $session.CompareDirectories(                     $session.CompareDirectories(
-                        [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $delete)+                        [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $delete
 +                        $False, [WinSCP.SynchronizationCriteria]::Time, $transferOptions)
                Write-Host                 Write-Host
Line 118: Line 134:
                {                 {
                    Write-Host "Synchronizing $($differences.Count) change(s)..."                     Write-Host "Synchronizing $($differences.Count) change(s)..."
 +                    SetConsoleTitle "Synchronizing changes"
                    Beep                     Beep
Line 144: Line 161:
                        try                         try
                        {                         {
-                            $difference.Resolve($session) | Out-Null+                            $difference.Resolve($session, $transferOptions) | Out-Null
                            Write-Host " Done."                             Write-Host " Done."
                        }                         }
Line 161: Line 178:
            }             }
-            Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."+            SetConsoleTitle "Waiting"
            $wait = [int]$interval             $wait = [int]$interval
            # Wait for 1 second in a loop, to make the waiting breakable             # Wait for 1 second in a loop, to make the waiting breakable
            while ($wait -gt 0)             while ($wait -gt 0)
            {             {
 +                Write-Host -NoNewLine "`rWaiting for $wait seconds, press Ctrl+C to abort... "
                Start-Sleep -Seconds 1                 Start-Sleep -Seconds 1
                $wait--                 $wait--
            }             }
 +            Write-Host
            Write-Host             Write-Host
        }         }
Line 175: Line 194:
    finally     finally
    {     {
 +        Write-Host # to break after "Waiting..." status
        Write-Host "Disconnecting..."         Write-Host "Disconnecting..."
        # Disconnect, clean up         # Disconnect, clean up
Line 184: Line 204:
    $continueOnError = $True     $continueOnError = $True
    HandleException $_     HandleException $_
 +    SetConsoleTitle "Error"
} }
Line 210: Line 231:
In the //Interval// box, specify an interval between the checks for changes. In the //Interval// box, specify an interval between the checks for changes.
 +
 +In the //File mask// box, you can specify [[file_mask|file mask]] to select/deselect files (or file types) and directories for the synchronization.
In the //Session log file//, you can specify a path to a [[logging|session log file]]. The option is available on the [[ui_pref_commands|Preferences dialog]] only. In the //Session log file//, you can specify a path to a [[logging|session log file]]. The option is available on the [[ui_pref_commands|Preferences dialog]] only.
Line 221: Line 244:
To make the synchronization run periodically, you can either [[guide_schedule|schedule the script]] or run the script in a loop. The latter may be more convenient, as it allows you to see all runs in a single console window, so that you can review the updates easily. To make the synchronization run periodically, you can either [[guide_schedule|schedule the script]] or run the script in a loop. The latter may be more convenient, as it allows you to see all runs in a single console window, so that you can review the updates easily.
-To run the script in the loop, you can use following batch file:((Note that the ''[[https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/timeout_1|timeout]]'' command is available since Windows 7 only. For alternatives in older versions of Windows, refer to [[https://stackoverflow.com/q/1672338/850848|How to sleep for 5 seconds in Windows's Command Prompt?]] article.))+To run the script in the loop, you can use following batch file:((Note that the ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/timeout|timeout]]'' command is available since Windows 7 only. For alternatives in older versions of Windows, refer to [[https://stackoverflow.com/q/1672338/850848|How to sleep for 5 seconds in Windows's Command Prompt?]] article.))
<code batch> <code batch>
@echo off @echo off

Last modified: by martin