Question

I have a script that is rather simple, it boots up WinSCP and checks the directory for a file that starts with "TSA". If the file exists, it exits, if it does not exist, it transfers over a new file.

Its up and running on my Windows 7 machine, that is where i created it - but when i transfer it over to my server [windows server 2003] it never finds the file.

My script:

var FILEPATH = "../zfinance/TSA";

// Session to connect to
var SESSION = "someplace@somewhere.com";

// 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;

// run winscp to check for 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 the script finishes
while (exec.Status == 0)
{
    WScript.Sleep(100);
    WScript.Echo(exec.StdOut.ReadAll());
}
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'");

doc.setProperty("SelectionLanguage", "XPath");             
var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, '" + filename + "')]");
if (nodes.length > 0)
{
    WScript.Echo("File found");
    WScript.Quit(0);
}
else
{
    WScript.Echo("File not found");       
    WScript.Quit(1);
}

After much investigation, i think i've found the piece of code that does not function properly:

// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument.6.0");
doc.async = false;
doc.load(logfilepath);
doc.setProperty("SelectionNamespaces",
    "xmlns:w='http://winscp.net/schema/session/1.0'");

The only problem is, i have no idea why. The log file at this point should be written over with the xml code, but this does not happen.

Thanks in advance for any help.

Was it helpful?

Solution

And the answer is........... WinSCP on Windows Server 2003 was WAY out of date. So out of date that the log was completely different from one version to the next. Updated and VIOLA! Problem solved. Thanks for your help.

OTHER TIPS

Maybe you need to install MSXML2.DOMDocument.6.0

http://msdn.microsoft.com/en-us/library/windows/desktop/cc507436%28v=vs.85%29.aspx

If you open up regedit and look for "MSXML2.DOMDocument.6.0" does it find it? If so maybe security settings for the script need to be set in order to be able to create an activeX object.

What can you see when you put some stuff in try catch?

try{
//stuff
}catch(e){
  WScript.Echo(e.message);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top