簡體   English   中英

批處理文件> Javascript> WinSCP>檢查文件是否存在

[英]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-withcontains

選擇語言屬性 (MDSN)

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM