【问题标题】:Batch File > Javascript > WinSCP > Check if file exists批处理文件 > Javascript > WinSCP > 检查文件是否存在
【发布时间】:2025-12-12 11:40:02
【问题描述】:

我有一个批处理文件,它将启动一个 .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')]");

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

【问题讨论】:

    标签: javascript linux batch-file batch-processing winscp


    【解决方案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-withcontains 等方法。

    SelectionLanguage Property (MDSN).

    【讨论】:

    • 关闭!我收到一条错误消息,提示您使用“starts-with”修改的那一行上的未知方法。
    • 我已经尝试了我能想到的所有可能的组合。我仍然没有让它工作。有没有办法只在xml文件中搜索TSS?
    • var nodes = doc.selectNodes("//w:file/w:filename[starts-with(@value, 'TSA')]"); 应该可以,不是吗?
    • 如果我没有犯任何错误,那么是的。我现在不知道是什么导致了错误。
    • @user2241406 您可以尝试将starts-with 替换为contains
    【解决方案2】:

    您可以使用stat command。您甚至可以将 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 assemblySession.FileExists


    更多详细信息,请参阅 WinSCP 文章Checking file existence

    【讨论】:

    • 这确实会让我的生活更轻松,假设我什至知道如何编写代码而不是 hello world。