Differences

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

2016-03-07 2016-03-07
simplifying PowerShell example (martin) simplifying JScript example (martin)
Line 229: Line 229:
        session.Open(sessionOptions);         session.Open(sessionOptions);
-        var today = new Date();  +        // Download files 
-        var stamp = +        var transferOptions = WScript.CreateObject("WinSCP.TransferOptions")
-           today.getFullYear() + + ·······transferOptions.TransferMode = TransferMode_Binary; 
-            (today.getMonth() + 1 < 10 ? "0&quot; : "";) + (today.getMonth() + 1) + +  
- ···········(today.getDate() < 10 ? ";0" : "") + today.getDate(); +        var transferResult = session.GetFiles("/home/user/*", "d:\\download\\", false, transferOptions); 
-        var fileName = "export_" + stamp + ".txt&quot;; + ········ 
-        var remotePath = "/home/user/sysbatch/" + fileName+        // Throw on any error 
-        var localPath = "d:\\backup\\" + fileName+        transferResult.Check(); 
- +········· 
- ·······var fs = WScript.CreateObject("Scripting.FileSystemObject"); +        // Print results 
- ··············· +        for (var enumerator = new Enumerator(transferResult.Transfers); !enumerator.atEnd(); enumerator.moveNext())
-        // Manual "remote to local" synchronization. +
- +
-        // You can achieve the same using: +
-        // var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); +
-        // transferOptions.FileMask = fileName; +
-        // session.SynchronizeDirectories( +
-       //     SynchronizationMode_Local, localPath, remotePath, false, false, SynchronizationCriteria_Time,  +
-········// ····transferOptions).Check(); +
-       if (session.FileExists(remotePath))+
        {         {
-            var download; +            WScript.Echo("Download of " + enumerator.item().FileName + " succeeded");
-            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");+
        }         }
    }     }

Last modified: by martin