Differences
This shows you the differences between the selected revisions of the page.
2012-02-18 | 2012-02-22 | ||
proper indenting (martin) | vbscript example (martin) | ||
Line 275: | Line 275: | ||
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 not newer (" + remoteWriteTime + ") than local backup local backup (" + localWriteTime + ")"); | + | "but remote file is not newer (" + remoteWriteTime + ") than " + |
+ | "local backup local backup (" + localWriteTime + ")"); | ||
download = false; | download = false; | ||
} | } | ||
Line 292: | Line 293: | ||
WScript.Echo("File " + remotePath + " does not exist yet"); | WScript.Echo("File " + remotePath + " does not exist yet"); | ||
} | } | ||
+ | |||
+ | </script> | ||
+ | </job> | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ==== [[vbscript]] VBScript (WSH) Example ==== | ||
+ | In this example the VBScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]]. | ||
+ | |||
+ | <code vb> | ||
+ | <job> | ||
+ | <reference object="WinSCP.Session"/> | ||
+ | <script language="VBScript"> | ||
+ | |||
+ | ' Setup session options | ||
+ | Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions") | ||
+ | sessionOptions.Protocol = Protocol_Sftp | ||
+ | sessionOptions.HostName = "example.com" | ||
+ | sessionOptions.UserName = "user" | ||
+ | sessionOptions.Password = "mypassword" | ||
+ | sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
+ | |||
+ | Set session = WScript.CreateObject("WinSCP.Session") | ||
+ | |||
+ | session.DebugLogPath = "debug.log" | ||
+ | session.DisableVersionCheck = True | ||
+ | |||
+ | ' Connect | ||
+ | session.Open sessionOptions | ||
+ | |||
+ | today = Date | ||
+ | stamp = Year(today) | ||
+ | If Month(today) < 10 Then | ||
+ | stamp = stamp & "0" | ||
+ | End If | ||
+ | stamp = stamp & Month(today) | ||
+ | if Day(today) < 10 Then | ||
+ | stamp = stamp & "0" | ||
+ | End If | ||
+ | stamp = stamp & Day(today) | ||
+ | |||
+ | fileName = "export_" & stamp & ".txt" | ||
+ | remotePath = "/home/user/sysbatch/" & fileName | ||
+ | localPath = "d:\backup\" & fileName | ||
+ | |||
+ | Set fs = WScript.CreateObject("Scripting.FileSystemObject") | ||
+ | |||
+ | ' Manual "remote to local" synchronization. | ||
+ | |||
+ | ' You can achieve the same using: | ||
+ | ' Set transferOptions = WScript.CreateObject("WinSCP.TransferOptions") | ||
+ | ' transferOptions.IncludeMask = fileName | ||
+ | ' session.SynchronizeDirectories( _ | ||
+ | ' SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time, _ | ||
+ | ' transferOptions).Check | ||
+ | If session.FileExists(remotePath) Then | ||
+ | If Not fs.FileExists(localPath) Then | ||
+ | WScript.Echo "File " & remotePath & " exists, local backup " & localPath & " does not" | ||
+ | download = True | ||
+ | Else | ||
+ | remoteWriteTime = CDate(session.GetFileInfo(remotePath).LastWriteTime) | ||
+ | localWriteTime = fs.GetFile(localPath).DateLastModified | ||
+ | |||
+ | If remoteWriteTime > localWriteTime Then | ||
+ | WScript.Echo _ | ||
+ | "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | ||
+ | "but remote file is newer (" & remoteWriteTime & ") than local backup (" & localWriteTime & ")" | ||
+ | download = True | ||
+ | Else | ||
+ | WScript.Echo _ | ||
+ | "File " & remotePath & " as well as local backup " & localPath & " exist, " & _ | ||
+ | "but remote file is not newer (" & remoteWriteTime & ") than " & _ | ||
+ | "local backup local backup (" & localWriteTime & ")" | ||
+ | download = False | ||
+ | End If | ||
+ | End If | ||
+ | |||
+ | If download Then | ||
+ | ' Download the file and throw on any error | ||
+ | session.GetFiles(remotePath, localPath).Check() | ||
+ | |||
+ | WScript.Echo "Download to backup done." | ||
+ | End If | ||
+ | Else | ||
+ | WScript.Echo "File " & remotePath & " does not exist yet" | ||
+ | End If | ||
</script> | </script> |