Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

CBW

The problem was simple. Server 2003 had not had WinSCP updated in a long time. I updated it and the log generated xml as desired. Problem solved! Thanks. I now have a 100% working script that checks if a file exists, then acts accordingly.
martin

Re: Script works on Win 7 but Not Server 2003

1) Consider using Session.FileExists from WinSCP .NET assembly instead:
https://winscp.net/eng/docs/library
https://winscp.net/eng/docs/library_session_fileexists

PowerShell script may be the easiest to use for you:
https://winscp.net/eng/docs/library_session_fileexists#powershell

2) Share the .XML log with us (logfilepath) + make WinSCP generate session log and share that too:

exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\" /log=session.log");
CBW

Script works on Win 7 but Not Server 2003

I have this script working, as was posted in the guides section, to check if a file exists. It works fantastically on Windows 7, but on server 2003 it will never return if the file is found. Any ideas?

Here is the 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.6.0");
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");
    // 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");        //this is always the result on jack.ser
    WScript.Quit(1);
}