Differences
This shows you the differences between the selected revisions of the page.
2014-10-24 | 2014-11-06 | ||
typo (martin) | we can now simply test file existence in scripting using stat command (martin) | ||
Line 405: | Line 405: | ||
~~AD~~ | ~~AD~~ | ||
- | === [[scripting]] Using WinSCP Scripting and XML Logging from JScript === | + | === [[scripting]] Using WinSCP Scripting === |
- | You may use following [[guide_automation_advanced#wsh|Windows script host JScript code]] (''example.js''): | + | You can use a ''[[scriptcommand_stat|stat]]'' command in ''[[scriptcommand_option|option batch abort]]'' mode to query file attributes. If the file does not exist, the ''stat'' command fails and so does the script. Then, [[scripting#checking_results|test WinSCP exit code]] to determine, if the file exists or not. |
- | <code javascript> | + | <code batch> |
- | // Configuration | + | @echo off |
- | + | ||
- | // Remote file search for | + | |
- | var FILEPATH = "/home/user/myfile.dat"; | + | |
- | // 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 p = FILEPATH.lastIndexOf('/'); | + | |
- | var path = FILEPATH.substring(0, p); | + | |
- | var filename = FILEPATH.substring(p + 1); | + | |
- | var exec; | + | set REMOTE_PATH=/home/user/test.txt |
- | + | winscp.com /command "option batch abort" "open mysession" "stat %REMOTE_PATH%" "exit" | |
- | // run winscp to check for file existence | + | |
- | exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\""); | + | |
- | exec.StdIn.Write( | + | |
- | ···"option batch abort\n" + | + | |
- | ····"open \"" + SESSION + "\"\n" + | + | |
- | ····"ls \"" + path + "\"\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 checking for file existence"); | + | |
- | 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/w:filename[@value='" + filename + "']"); | + | |
- | + | ||
- | if (nodes.length > 0) | + | |
- | { | + | |
- | WScript.Echo("File found"); | + | |
- | // signalize file existence to calling process; | + | |
- | // you can also continue with processing (e.g. downloading the file) | + | |
- | // directly from the script here | + | |
- | WScript.Quit(0); | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | WScript.Echo("File not found"); | + | |
- | WScript.Quit(1); | + | |
- | } | + | |
- | </code> | + | |
- | Run the script with command: | ||
- | <code batch> | ||
- | cscript /nologo example.js | ||
- | </code> | ||
- | |||
- | Following batch file shows how to continue with processing based on file existence: | ||
- | <code batch> | ||
- | cscript /nologo example.js | ||
if errorlevel 1 goto error | if errorlevel 1 goto error | ||
- | echo File exists, do something | + | echo File %REMOTE_PATH% exists |
rem Do something | rem Do something | ||
exit 0 | exit 0 | ||
:error | :error | ||
- | echo Error or file not exists | + | echo Error or file %REMOTE_PATH% not exists |
exit 1 | exit 1 | ||
</code> | </code> | ||
- | |||
- | Alternatively you can continue with the processing directly in JScript code as suggested by respective comments. | ||
==== [[local]] Local file existence ==== | ==== [[local]] Local file existence ==== |