简体   繁体   English

批处理文件> Javascript> WinSCP>检查文件是否存在

[英]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. 我有一个批处理文件,它将启动一个.js文件,该文件通过WinSCP检查文件是否存在,如果不存在,则返回到该批处理文件。

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
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. 因此,每十分钟,此批处理文件将运行,检查是否有文件,如果没有,请复制一个。

The file.js: 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);
}

On line 4 it says: 在第4行上它说:

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. 我需要查找一个以TSS开始的文件,但最后要加上一个时间戳。 So i need to just use a wildcard after TSS. 所以我只需要在TSS之后使用通配符即可。

So what i need help with is: Making this process return true if any file exists with TSS* 因此,我需要帮助的是:如果TSS *存在任何文件,则使此过程返回true

Any help would be much appreciated. 任何帮助将非常感激。

EDIT: 编辑:

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. 您需要在var nodes...行中更正xpath表达式。 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 . 并从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 . 注意:必须使用第一行才能将XPath用作查询语言,而不是默认的(和较旧的) XSLPattern ,它不支持诸如starts-withcontains

SelectionLanguage Property (MDSN) . 选择语言属性 (MDSN)

You can use the stat command . 您可以使用stat命令 You can even inline the WinSCP script into the batch file: 您甚至可以将WinSCP脚本内联到批处理文件中:

@echo off

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

if errorlevel 1 goto error

echo File %REMOTE_PATH% exists
rem Do something
exit 0

:error
echo Error or file %REMOTE_PATH% not exists
exit 1

An alternative is using the Session.FileExists from WinSCP .NET assembly . 一种替代方法是使用WinSCP .NET程序集中Session.FileExists


For further details, see the WinSCP article Checking file existence . 有关更多详细信息,请参见WinSCP文章“ 检查文件是否存在”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM