Differences

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

2012-02-18 2012-02-18
few formating new lines in vbnet (martin) jscript example (martin)
Line 210: Line 210:
End Class End Class
</code> </code>
 +
 +==== [[jscript]] JScript (WSH) Example ====
 +In this example the JScript script is embedded into WSF file, to allow [[library_com_wsh#enums|access to enumeration values]].
 +
 +<code javascript>
 +<job>                                                             
 +<reference object="WinSCP.Session"/>
 +<script language="JScript">
 +
 +// Setup session options
 +var 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";
 +
 +var session = WScript.CreateObject("WinSCP.Session");
 +
 +// Connect
 +session.Open(sessionOptions);
 +
 +var today = new Date();
 +var stamp = today.getFullYear() + (today.getMonth() + 1 < 10 ? "0" : "") + (today.getMonth() + 1) + (today.getDate() < 10 ? "0" : "") + today.getDate();
 +var fileName = "export_" + stamp + ".txt";
 +var remotePath = "/home/user/sysbatch/" + fileName;
 +var localPath = "d:\\backup\\" + fileName;
 +
 +var fs = WScript.CreateObject("Scripting.FileSystemObject");
 +       
 +// Manual "remote to local" synchronization.
 +
 +// You can achieve the same using:
 +// var transferOptions = WScript.CreateObject("WinSCP.TransferOptions");
 +// transferOptions.IncludeMask = fileName;
 +// session.SynchronizeDirectories(
 +//    SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time,
 +//    transferOptions).Check();
 +if (session.FileExists(remotePath))
 +{
 +    var download;
 +    if (!fs.FileExists(localPath))
 +    {
 +        WScript.Echo("File " + remotePath + " exists, local backup " + localPath + " does not");
 +        download = true;
 +    }
 +    else
 +    {
 +        var remoteWriteTime = new Date(session.GetFileInfo(remotePath).LastWriteTime);
 +        var localWriteTime = fs.GetFile(localPath).DateLastModified;
 +
 +        if (remoteWriteTime > localWriteTime)
 +        {
 +            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;
 +        }
 +    }
 +
 +    if (download)
 +    {
 +        // Download the file and throw on any error
 +        session.GetFiles(remotePath, localPath).Check();
 +
 +        WScript.Echo("Download to backup done.");
 +    }
 +}
 +else
 +{
 +    WScript.Echo("File " + remotePath + " does not exist yet");
 +}
 +
 +</script>
 +</job>
 +</code>
 +

Last modified: by martin