Differences

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

2012-03-21 2012-03-21
dispose in vbscript and jscript (martin) try catch finally in jscript (martin)
Line 220: Line 220:
<script language="JScript"> <script language="JScript">
-// Setup session options +try 
-var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); +
-sessionOptions.Protocol = Protocol_Sftp; +····// Setup session options 
-sessionOptions.HostName = "example.com"; +····var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); 
-sessionOptions.UserName = "user"; +····sessionOptions.Protocol = Protocol_Sftp; 
-sessionOptions.Password = "mypassword"; +····sessionOptions.HostName = "example.com"; 
-sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; +····sessionOptions.UserName = "user"; 
-  +····sessionOptions.Password = "mypassword"; 
-var session = WScript.CreateObject("WinSCP.Session");+····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 +    try 
-session.Open(sessionOptions);+    { 
 +········// Connect 
 +········session.Open(sessionOptions);
-var today = new Date();  +········var today = new Date();  
-var stamp = +········var stamp = 
- ···today.getFullYear() + + ···········today.getFullYear() + 
- ···(today.getMonth() + 1 < 10 ? "0" : "") + (today.getMonth() + 1) + + ···········(today.getMonth() + 1 < 10 ? "0" : "") + (today.getMonth() + 1) + 
- ···(today.getDate() < 10 ? "0" : "") + today.getDate(); + ···········(today.getDate() < 10 ? "0" : "") + today.getDate(); 
-var fileName = "export_" + stamp + ".txt"; +········var fileName = "export_" + stamp + ".txt"; 
-var remotePath = "/home/user/sysbatch/" + fileName; +········var remotePath = "/home/user/sysbatch/" + fileName; 
-var localPath = "d:\\backup\\" + fileName;+········var localPath = "d:\\backup\\" + fileName;
-var fs = WScript.CreateObject("Scripting.FileSystemObject"); +········var fs = WScript.CreateObject("Scripting.FileSystemObject"); 
- ······· + ··············· 
-// Manual "remote to local" synchronization.+········// Manual "remote to local" synchronization.
-// You can achieve the same using: +········// You can achieve the same using: 
-// var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); +········// var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); 
-// transferOptions.IncludeMask = fileName; +········// transferOptions.IncludeMask = fileName; 
-// session.SynchronizeDirectories( +········// session.SynchronizeDirectories( 
-//    SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time,  +········//    SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time,  
-//    transferOptions).Check(); +········//    transferOptions).Check(); 
-if (session.FileExists(remotePath)) +········if (session.FileExists(remotePath)) 
-+········
- ···var download; + ···········var download; 
- ···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; 
- ···+ ···········
- ···else + ···········else 
- ···+ ···········
- ·······var remoteWriteTime = new Date(session.GetFileInfo(remotePath).LastWriteTime); + ···············var remoteWriteTime = new Date(session.GetFileInfo(remotePath).LastWriteTime); 
- ·······var localWriteTime = fs.GetFile(localPath).DateLastModified;+ ···············var localWriteTime = fs.GetFile(localPath).DateLastModified;
-········if (remoteWriteTime > localWriteTime) +················if (remoteWriteTime > localWriteTime) 
- ·······+ ···············
- ···········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 
 +                { 
 +                    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         else
        {         {
-            WScript.Echo( +            WScript.Echo("File " + remotePath + " does not exist yet");
-················"File " + remotePath + " as well as local backup " + localPath + " exist, " + +
- ···············"but remote file is not newer (" + remoteWriteTime + ") than " + +
-                "local backup local backup (" + localWriteTime + ")"); +
-            download = false;+
        }         }
    }     }
- +····finally
-····if (download)+
    {     {
-        // Download the file and throw on any error +        // Disconnect, clean up 
-        session.GetFiles(remotePath, localPath).Check(); +        session.Dispose();
- +
-        WScript.Echo("Download to backup done.");+
    }     }
} }
-else+catch (e)
{ {
-    WScript.Echo("File " + remotePath + &quot; does not exist yet&quot;);+    WScript.Echo("Error: " + e.message); 
 + ···WScript.Quit(1);
} }
- 
-// Disconnect, clean up 
-session.Dispose(); 
</script> </script>

Last modified: by martin