Batch File > Javascript > WinSCP > Check if file exists

I have a batch file that will launch a .js file which, via WinSCP, checks if a file exists and returns to the batch file if it does or not.

The problem IS: It always returns not found, and I cannot figure out why. I am unsure how to use a wildcard in this scenario.

The batch file looks like this:

cscript /nologo file.js
if errorlevel 1 goto notfound
(another script to copy a file over)

Only one file can exist on the server at once. So every ten min, this batch file will run, check if there is a file, if not, copy one over.

The file.js:

// Configuration

// Remote file search for
var FILEPATH = "../filepath/TSS*";

// Session to connect to
var SESSION = "mysession@someplace.come";

// Path to
var WINSCP = "c:\\program files (x86)\\winscp\\";

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 + "\"");
"option batch abort\n" +
"open \"" + SESSION + "\"\n" +
"ls \"" + path + "\"\n" +

// wait until the script finishes
while (exec.Status == 0)

if (exec.ExitCode != 0)
WScript.Echo("Error checking for file existence");

// look for log file
var logfile = filesys.GetFile(logfilepath);

if (logfile == null)
WScript.Echo("Cannot find log file");

// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;


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.Echo("File not found");

On line 4 it says:

var FILEPATH = "../filepath/TSS*";

That star is what is giving me issues, i think. I need to look for a file which STARTS WITH TSS, but will have a time stamp tacked on the end. So i need to just use a wildcard after TSS.

So what i need help with is: Making this process return true if any file exists with TSS*

Any help would be much appreciated.


var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, 'TSS')]");

This code seems to not work. If this code worked, it seems like it would solve all my problems.


You need to correct xpath expression in var nodes... line. Try something like this:

doc.setProperty("SelectionLanguage", "XPath"); //added in edit
var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, '" + filename + "')]");

and delete asterisk from FILEPATH.

Note: first line is required in order to use XPath as the query language, not default (and old) XSLPattern which doesn't support methods such as starts-with or contains.

SelectionLanguage Property (MDSN).

You can use stat command. You can even inline the WinSCP script into the batch file:

@echo off

set REMOTE_PATH=/home/user/test.txt /command ^
    "option batch abort" ^
    "open mysession" ^ 
    "stat %REMOTE_PATH%" ^ 

if errorlevel 1 goto error

echo File %REMOTE_PATH% exists
rem Do something
exit 0

echo Error or file %REMOTE_PATH% not exists
exit 1

Alternative is using Session.FileExists from WinSCP .NET assembly.

For further details, see WinSCP article Checking file existence.

Need Your Help

Distributed database, many lightly loaded nodes

couchdb cassandra distributed

I'm working on a hobby project involving a rather CPU-intensive calculation. The problem is embarrassingly parallel. This calculation will need to happen on a large number of nodes (say 1000-10000)...

HTML <div> tag used for getting a scroll bar

html css

i have a div section used for a scroll bar,but i am stuck with changing the width and height for the div section ,whatever i change no result.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.