linux - Batch File > Javascript > WinSCP > Check if file exists -
i have batch file launch .js file which, via winscp, checks if file exists , returns batch file if or not.
the problem is: returns not found, , cannot figure out why. unsure how use wildcard in scenario.
the batch file looks this:
cscript /nologo file.js if errorlevel 1 goto notfound exit :notfound (another script copy file over)
only 1 file can exist on server @ once. every ten min, batch file run, check if there file, if not, copy 1 over.
the file.js:
// configuration // remote file search var filepath = "../filepath/tss*"; // session connect var session = "mysession@someplace.come"; // path winscp.com var winscp = "c:\\program files (x86)\\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; // run winscp check file existence exec = shell.exec("\"" + winscp + "\" /log=\"" + logfilepath + "\""); exec.stdin.write( "option batch abort\n" + "open \"" + session + "\"\n" + "ls \"" + path + "\"\n" + "exit\n"); // wait until script finishes while (exec.status == 0) { wscript.sleep(100); wscript.echo(exec.stdout.readall()); } if (exec.exitcode != 0) { wscript.echo("error checking file existence"); wscript.quit(1); } // 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 calling process; // can continue processing (e.g. downloading file) // directly script here wscript.quit(0); } else { wscript.echo("file not found"); wscript.quit(1); }
on line 4 says:
var filepath = "../filepath/tss*";
that star giving me issues, think. need file starts tss, have time stamp tacked on end. need use wildcard after tss.
so need is: making process return true if file exists tss*
any appreciated.
edit:
var nodes = doc.selectnodes("//w:file/w:filename[starts-with(@value, 'tss')]");
this code seems not work. if code worked, seems solve problems.
you need correct xpath expression in var nodes...
line. try this:
doc.setproperty("selectionlanguage", "xpath"); //added in edit var nodes = doc.selectnodes("//w:file/w:filename[starts-with(@value, '" + filename + "')]");
and delete asterisk filepath
.
note: first line required in order use xpath
query language, not default (and old) xslpattern
doesn't support methods such starts-with
or contains
.
Comments
Post a Comment