[英]Batch File > Javascript > WinSCP > Check if file exists
我有一个批处理文件,它将启动一个.js文件,该文件通过WinSCP检查文件是否存在,如果不存在,则返回到该批处理文件。
问题是:总是返回找不到,而且我不知道为什么。 我不确定在这种情况下如何使用通配符。
批处理文件如下所示:
cscript /nologo file.js
if errorlevel 1 goto notfound
exit
:notfound
(another script to copy a file 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之后使用通配符即可。
因此,我需要帮助的是:如果TSS *存在任何文件,则使此过程返回true
任何帮助将非常感激。
编辑:
var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, 'TSS')]");
此代码似乎不起作用。 如果此代码有效,似乎可以解决我所有的问题。
您需要在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-with
或contains
。
您可以使用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
一种替代方法是使用WinSCP .NET程序集中的Session.FileExists
。
有关更多详细信息,请参见WinSCP文章“ 检查文件是否存在” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.