1

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

问题是:它总是返回 not found,我不知道为什么。我不确定如何在这种情况下使用通配符。

批处理文件如下所示:

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

服务器上一次只能存在一个文件。所以每隔十分钟,这个批处理文件就会运行一次,检查是否有文件,如果没有,复制一份。

文件.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 之后使用通配符。

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

任何帮助将非常感激。

编辑:

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

这段代码似乎不起作用。如果这段代码有效,它似乎可以解决我所有的问题。

4

2 回答 2

1

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

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

并从中删除星号FILEPATH

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

选择语言属性(MDSN)

于 2013-05-02T18:59:14.123 回答
1

你可以使用stat命令。您甚至可以将 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

另一种方法是使用Session.FileExists来自WinSCP .NET 程序集


有关详细信息,请参阅 WinSCP 文章检查文件存在

于 2013-05-07T08:50:29.917 回答