批处理文件> JavaScript> WinSCP>检查文件是否存在 [英] Batch File > Javascript > WinSCP > Check if file exists

查看:107
本文介绍了批处理文件> JavaScript> WinSCP>检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,它将启动一个.js文件,该文件通过WinSCP检查文件是否存在,如果不存在,则返回到该批处理文件.

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.

批处理文件如下:

cscript /nologo file.js
if errorlevel 1 goto notfound
exit
: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.

file.js:

// Configuration

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

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

// Path to 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 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'");

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);
}

第4行显示:

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

我认为那颗星正在给我带来麻烦.我需要查找一个以TSS开始的文件,但最后要加上一个时间戳.所以我只需要在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.

所以我需要帮助的是:如果TSS *存在任何文件,则使此过程返回true

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.

推荐答案

您需要在var nodes...行中更正xpath表达式. 尝试这样的事情:

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 + "')]");

,然后从FILEPATH中删除星号.

注意:为了使用XPath作为查询语言,第一行是必需的,而不是默认的(旧的)XSLPattern,它不支持诸如starts-withcontains之类的方法.

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属性(MDSN).

SelectionLanguage Property (MDSN).

这篇关于批处理文件> JavaScript> WinSCP>检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆