Differences
This shows you the differences between the selected revisions of the page.
| 2016-04-29 | 2016-07-29 | ||
| no summary (martin) | 5.9 stable released (martin) | ||
| Line 130: | Line 130: | ||
| ===== [[scripting]] Using WinSCP Scripting ===== | ===== [[scripting]] Using WinSCP Scripting ===== | ||
| - | ==== In the Beta Version ==== | ||
| - | &beta | ||
| - | |||
| Use the ''[[scriptcommand_get#latest|-latest]]'' switch of the ''[[scriptcommand_get|get]]'' command: | Use the ''[[scriptcommand_get#latest|-latest]]'' switch of the ''[[scriptcommand_get|get]]'' command: | ||
| <code winscp> | <code winscp> | ||
| get -latest /home/user/* c:\downloaded\ | get -latest /home/user/* c:\downloaded\ | ||
| - | </code> | ||
| - | |||
| - | ==== In the Stable Version ==== | ||
| - | |||
| - | You may use following [[guide_automation_advanced#wsh|Windows script host JScript code]] (''example.js''): | ||
| - | |||
| - | <code javascript> | ||
| - | // Configuration | ||
| - | |||
| - | // Local path to download to (keep trailing slash) | ||
| - | var LOCALPATH = "c:\\downloaded\\"; | ||
| - | // Remote path to search in (keep trailing slash) | ||
| - | var REMOTEPATH = "/home/user/"; | ||
| - | // Mask of files to search for | ||
| - | var FILEMASK = "*.*"; | ||
| - | // Session to connect to | ||
| - | var SESSION = "session"; | ||
| - | // Path to winscp.com | ||
| - | var WINSCP = "c:\\program files\\winscp\\winscp.com"; | ||
| - | |||
| - | var filesys = WScript.CreateObject("Scripting.FileSystemObject"); | ||
| - | var shell = WScript.CreateObject("WScript.Shell"); | ||
| - | |||
| - | var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml"; | ||
| - | |||
| - | var exec; | ||
| - | |||
| - | // run winscp to get list of file in the remote directory into XML log | ||
| - | exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\""); | ||
| - | exec.StdIn.Write( | ||
| - | "option batch abort\n" + | ||
| - | "open \"" + SESSION + "\"\n" + | ||
| - | "ls \"" + REMOTEPATH + FILEMASK + "\"\n" + | ||
| - | "exit\n"); | ||
| - | |||
| - | // wait until it finishes and collect its output | ||
| - | var output = exec.StdOut.ReadAll(); | ||
| - | // optionally print the output | ||
| - | WScript.Echo(output); | ||
| - | |||
| - | if (exec.ExitCode != 0) | ||
| - | { | ||
| - | WScript.Echo("Error retrieving list of files"); | ||
| - | WScript.Quit(1); | ||
| - | } | ||
| - | |||
| - | // look for log file | ||
| - | var logfile = filesys.GetFile(logfilepath); | ||
| - | |||
| - | if (logfile == null) | ||
| - | { | ||
| - | WScript.Echo("Cannot find log file"); | ||
| - | WScript.Quit(1); | ||
| - | } | ||
| - | |||
| - | // parse XML log file | ||
| - | var doc = new ActiveXObject("MSXML2.DOMDocument"); | ||
| - | doc.async = false; | ||
| - | doc.load(logfilepath); | ||
| - | |||
| - | doc.setProperty("SelectionNamespaces", | ||
| - | "xmlns:w='http://winscp.net/schema/session/1.0'"); | ||
| - | |||
| - | var nodes = doc.selectNodes("//w:file"); | ||
| - | |||
| - | // find the latest file | ||
| - | var filenameLatest = null; | ||
| - | var modificationLatest = null; | ||
| - | for (var i = 0; i < nodes.length; ++i) | ||
| - | { | ||
| - | var filename = nodes[i].selectSingleNode("w:filename/@value"); | ||
| - | var modification = nodes[i].selectSingleNode("w:modification/@value"); | ||
| - | if ((filename != null) && | ||
| - | (filename.value != ".") && | ||
| - | (filename.value != "..") && | ||
| - | (modification != null)) | ||
| - | { | ||
| - | // can compare timestamps stringwise | ||
| - | if ((modificationLatest == null) || | ||
| - | (modificationLatest < modification.value)) | ||
| - | { | ||
| - | modificationLatest = modification.value; | ||
| - | filenameLatest = filename.value; | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | // no file in the log | ||
| - | if (filenameLatest == null) | ||
| - | { | ||
| - | WScript.Echo("No file found"); | ||
| - | WScript.Quit(0); | ||
| - | } | ||
| - | |||
| - | // run winscp to download the latest file | ||
| - | exec = shell.Exec("\"" + WINSCP + "\""); | ||
| - | exec.StdIn.Write( | ||
| - | "option batch abort\n" + | ||
| - | "option confirm off\n" + | ||
| - | "open \"" + SESSION + "\"\n" + | ||
| - | "get \"" + REMOTEPATH + filenameLatest + "\" \"" + LOCALPATH + "\"\n" + | ||
| - | "exit\n"); | ||
| - | |||
| - | // wait until it finishes and collect its output | ||
| - | var output = exec.StdOut.ReadAll(); | ||
| - | // optionally print the output | ||
| - | WScript.Echo(output); | ||
| - | |||
| - | if (exec.ExitCode != 0) | ||
| - | { | ||
| - | WScript.Echo("Error downloading " + filenameLatest); | ||
| - | WScript.Quit(1); | ||
| - | } | ||
| - | </code> | ||
| - | |||
| - | Run the script with command: | ||
| - | <code batch> | ||
| - | cscript /nologo example.js | ||
| </code> | </code> | ||