Differences
This shows you the differences between the selected revisions of the page.
| library_example_check_existence_timestamp 2016-04-29 | library_example_check_existence_timestamp 2022-06-16 (current) | ||
| Line 24: | Line 24: | ||
| UserName = "user", | UserName = "user", | ||
| Password = "mypassword", | Password = "mypassword", | ||
| - | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| }; | }; | ||
| Line 35: | Line 35: | ||
| string fileName = "export_" + stamp + ".txt"; | string fileName = "export_" + stamp + ".txt"; | ||
| string remotePath = "/home/user/sysbatch/" + fileName; | string remotePath = "/home/user/sysbatch/" + fileName; | ||
| - | string localPath = "d:\\backup\\" + fileName; | + | string localPath = @"d:\backup\" + fileName; |
| // Manual "remote to local" synchronization. | // Manual "remote to local" synchronization. | ||
| Line 41: | Line 41: | ||
| // You can achieve the same using: | // You can achieve the same using: | ||
| // session.SynchronizeDirectories( | // session.SynchronizeDirectories( | ||
| - | // SynchronizationMode.Local, localPath, remotePath, false, false, SynchronizationCriteria.Time,· | + | // SynchronizationMode.Local, localPath, remotePath, false, false, |
| + | // SynchronizationCriteria.Time, | ||
| // new TransferOptions { FileMask = fileName }).Check(); | // new TransferOptions { FileMask = fileName }).Check(); | ||
| if (session.FileExists(remotePath)) | if (session.FileExists(remotePath)) | ||
| Line 48: | Line 49: | ||
| if (!File.Exists(localPath)) | if (!File.Exists(localPath)) | ||
| { | { | ||
| - | Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath); | + | Console.WriteLine( |
| + | ····························"File {0} exists, local backup {1} does not", | ||
| + | ···························remotePath, localPath); | ||
| download = true; | download = true; | ||
| } | } | ||
| else | else | ||
| { | { | ||
| - | DateTime remoteWriteTime = session.GetFileInfo(remotePath).LastWriteTime; | + | DateTime remoteWriteTime = |
| + | ···························session.GetFileInfo(remotePath).LastWriteTime; | ||
| DateTime localWriteTime = File.GetLastWriteTime(localPath); | DateTime localWriteTime = File.GetLastWriteTime(localPath); | ||
| Line 117: | Line 121: | ||
| .UserName = "user" | .UserName = "user" | ||
| .Password = "mypassword" | .Password = "mypassword" | ||
| - | .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| End With | End With | ||
| Line 124: | Line 128: | ||
| session.Open(sessionOptions) | session.Open(sessionOptions) | ||
| - | Dim stamp As String = DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture) | + | Dim stamp As String = |
| + | ···················DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture) | ||
| Dim fileName As String = "export_" & stamp & ".txt" | Dim fileName As String = "export_" & stamp & ".txt" | ||
| Dim remotePath As String = "/home/user/sysbatch/" & fileName | Dim remotePath As String = "/home/user/sysbatch/" & fileName | ||
| Line 132: | Line 137: | ||
| ' You can achieve the same using: | ' You can achieve the same using: | ||
| - | ' session.SynchronizeDirectories( _ | + | ' session.SynchronizeDirectories( |
| - | ' SynchronizationMode.Local, localPath, remotePath, False, False, SynchronizationCriteria.Time, _ | + | ' SynchronizationMode.Local, localPath, remotePath, False, False, |
| + | ' SynchronizationCriteria.Time, | ||
| ' New TransferOptions With { .FileMask = fileName }).Check | ' New TransferOptions With { .FileMask = fileName }).Check | ||
| If session.FileExists(remotePath) Then | If session.FileExists(remotePath) Then | ||
| Dim download As Boolean | Dim download As Boolean | ||
| If Not File.Exists(localPath) Then | If Not File.Exists(localPath) Then | ||
| - | Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath) | + | Console.WriteLine( |
| + | ····························"File {0} exists, local backup {1} does not", | ||
| + | ···························remotePath, localPath) | ||
| download = True | download = True | ||
| Else | Else | ||
| - | Dim remoteWriteTime As DateTime = session.GetFileInfo(remotePath).LastWriteTime | + | Dim remoteWriteTime As DateTime = |
| + | ···························session.GetFileInfo(remotePath).LastWriteTime | ||
| Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath) | Dim localWriteTime As DateTime = File.GetLastWriteTime(localPath) | ||
| If remoteWriteTime > localWriteTime Then | If remoteWriteTime > localWriteTime Then | ||
| - | Console.WriteLine( _ | + | Console.WriteLine( |
| - | "File {0} as well as local backup {1} exist, " & _ | + | "File {0} as well as local backup {1} exist, " & |
| - | "but remote file is newer ({2}) than local backup ({3})", _ | + | "but remote file is newer ({2}) than " & |
| + | "local backup ({3})", | ||
| remotePath, localPath, remoteWriteTime, localWriteTime) | remotePath, localPath, remoteWriteTime, localWriteTime) | ||
| download = True | download = True | ||
| Else | Else | ||
| - | Console.WriteLine( _ | + | Console.WriteLine( |
| - | "File {0} as well as local backup {1} exist, " & _ | + | "File {0} as well as local backup {1} exist, " & |
| - | "but remote file is not newer ({2}) than local backup ({3})", _ | + | "but remote file is not newer ({2}) than " & |
| + | "local backup ({3})", | ||
| remotePath, localPath, remoteWriteTime, localWriteTime) | remotePath, localPath, remoteWriteTime, localWriteTime) | ||
| download = False | download = False | ||
| Line 196: | Line 207: | ||
| UserName = "user" | UserName = "user" | ||
| Password = "mypassword" | Password = "mypassword" | ||
| - | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| } | } | ||
| Line 206: | Line 217: | ||
| $session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
| - | $stamp = Get-Date -f "yyyyMMdd" | + | $stamp = Get-Date -Format "yyyyMMdd" |
| $fileName = "export_$stamp.txt" | $fileName = "export_$stamp.txt" | ||
| $remotePath = "/home/user/sysbatch/$fileName" | $remotePath = "/home/user/sysbatch/$fileName" | ||
| Line 225: | Line 236: | ||
| { | { | ||
| Write-Host ( | Write-Host ( | ||
| - | "File {0} exists, local backup {1} does not" -f | + | "File $remotePath exists, local backup $localPath does not") |
| - | $remotePath, $localPath) | + | |
| $download = $True | $download = $True | ||
| } | } | ||
| Line 236: | Line 246: | ||
| if ($remoteWriteTime -gt $localWriteTime) | if ($remoteWriteTime -gt $localWriteTime) | ||
| { | { | ||
| - | Write-Host ( | + | Write-Host ("File $remotePath as well as local backup $localPath exist, " + |
| - | ························("File {0} as well as local backup {1} exist, " + | + | ·······················"but remote file is newer ($remoteWriteTime) than " + |
| - | ························"but remote file is newer ({2}) than local backup ({3})") -f | + | "local backup ($localWriteTime)") |
| - | $remotePath, $localPath, $remoteWriteTime, $localWriteTime) | + | |
| $download = $True | $download = $True | ||
| } | } | ||
| Line 245: | Line 254: | ||
| { | { | ||
| Write-Host ( | Write-Host ( | ||
| - | ("File {0} as well as local backup {1} exist, " + | + | "File $remotePath as well as local backup $localPath exist, " + |
| - | ························"but remote file is not newer ({2}) than local backup ({3})") -f | + | ·······················"but remote file is not newer ($remoteWriteTime) than " + |
| - | $remotePath, $localPath, $remoteWriteTime, $localWriteTime) | + | "local backup ($localWriteTime)") |
| $download = $False | $download = $False | ||
| } | } | ||
| Line 262: | Line 271: | ||
| else | else | ||
| { | { | ||
| - | Write-Host ("File {0} does not exist yet" -f $remotePath) | + | Write-Host "File $remotePath does not exist yet" |
| } | } | ||
| } | } | ||
| Line 273: | Line 282: | ||
| exit 0 | exit 0 | ||
| } | } | ||
| - | catch [Exception] | + | catch |
| { | { | ||
| - | Write-Host ("Error: {0}" -f $_.Exception.Message) | + | Write-Host "Error: $($_.Exception.Message)" |
| exit 1 | exit 1 | ||
| } | } | ||
| Line 296: | Line 305: | ||
| sessionOptions.UserName = "user"; | sessionOptions.UserName = "user"; | ||
| sessionOptions.Password = "mypassword"; | sessionOptions.Password = "mypassword"; | ||
| - | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."; |
| var session = WScript.CreateObject("WinSCP.Session"); | var session = WScript.CreateObject("WinSCP.Session"); | ||
| Line 322: | Line 331: | ||
| // transferOptions.FileMask = fileName; | // transferOptions.FileMask = fileName; | ||
| // session.SynchronizeDirectories( | // session.SynchronizeDirectories( | ||
| - | // SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time, | + | // SynchronizationMode_Local, localPath, remotePath, false, false, |
| - | // transferOptions).Check(); | + | // SynchronizationCriteria_Time, transferOptions).Check(); |
| if (session.FileExists(remotePath)) | if (session.FileExists(remotePath)) | ||
| { | { | ||
| Line 329: | Line 338: | ||
| if (!fs.FileExists(localPath)) | if (!fs.FileExists(localPath)) | ||
| { | { | ||
| - | WScript.Echo("File " + remotePath + " exists, local backup " + localPath + " does not"); | + | WScript.Echo( |
| + | ····················"File " + remotePath + " exists, local backup " + localPath + " does not"); | ||
| download = true; | download = true; | ||
| } | } | ||
| Line 340: | Line 350: | ||
| { | { | ||
| WScript.Echo( | WScript.Echo( | ||
| - | "File " + remotePath + " as well as local backup " + localPath + " exist, " + | + | "File " + remotePath + " as well as local backup " + localPath + |
| - | "but remote file is newer (" + remoteWriteTime + ") than local backup (" + localWriteTime + ")"); | + | " exist, but remote file is newer (" + remoteWriteTime + ") than " + |
| + | "local backup (" + localWriteTime + ")"); | ||
| download = true; | download = true; | ||
| } | } | ||
| Line 347: | Line 358: | ||
| { | { | ||
| WScript.Echo( | WScript.Echo( | ||
| - | "File " + remotePath + " as well as local backup " + localPath + " exist, " + | + | "File " + remotePath + " as well as local backup " + localPath + |
| - | "but remote file is not newer (" + remoteWriteTime + ") than " + | + | ·······················" exist, but remote file is not newer (" + remoteWriteTime + ") than " + |
| - | "local backup local backup (" + localWriteTime + ")"); | + | "local backup (" + localWriteTime + ")"); |
| download = false; | download = false; | ||
| } | } | ||
| Line 402: | Line 413: | ||
| .UserName = "user" | .UserName = "user" | ||
| .Password = "mypassword" | .Password = "mypassword" | ||
| - | .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| End With | End With | ||
| Line 438: | Line 449: | ||
| ' transferOptions.FileMask = fileName | ' transferOptions.FileMask = fileName | ||
| ' session.SynchronizeDirectories( _ | ' session.SynchronizeDirectories( _ | ||
| - | ' SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time, _ | + | ' SynchronizationMode_Local, localPath, remotePath, false, false, _ |
| - | ' transferOptions).Check | + | ' SynchronizationCriteria_Time, transferOptions).Check |
| Dim download, remoteWriteTime, localWriteTime | Dim download, remoteWriteTime, localWriteTime | ||
| Line 454: | Line 465: | ||
| WScript.Echo _ | WScript.Echo _ | ||
| "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | ||
| - | "but remote file is newer (" & remoteWriteTime & ") than local backup (" & localWriteTime & ")" | + | "but remote file is newer (" & remoteWriteTime & ") than " & _ |
| + | "local backup (" & localWriteTime & ")" | ||
| download = True | download = True | ||
| Else | Else | ||
| Line 460: | Line 472: | ||
| "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | ||
| "but remote file is not newer (" & remoteWriteTime & ") than " & _ | "but remote file is not newer (" & remoteWriteTime & ") than " & _ | ||
| - | "local backup local backup (" & localWriteTime & ")" | + | "local backup (" & localWriteTime & ")" |
| download = False | download = False | ||
| End If | End If | ||