我無法顯示訊息“找不到檔案!” 使用 glob 。這里我使用 glob 來匹配檔案格式。如果檔案在目錄中可用,則它作業正常。但如果不可用,則不會顯示“找不到檔案”。我注意到 if 條件if (err)始終為 false ,因此無論是否可用,它始終顯示“找到檔案” !
const glob = require('glob');
value1='./**/**.NIC.2*' //file format like
if (value1){
const fs = require("fs");
glob(value1, function (err, files) {
if (err) {
console.log('File Not Found !');
}
else {
files.forEach(path => {
console.log('File Found !');
console.log('File Path => ' path);
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(path),
});
var lineCounter = 0; var wantedLines = [];
lineReader.on('line', function (line) {
lineCounter ;
wantedLines.push(line);
if(lineCounter==1){
lineReader.close();}
});
lineReader.on('close', function() {
var obj = wantedLines;
a=JSON.stringify(obj);
b=a.replace(/\s/g, "");
function2(b);
});
});
}});
}
uj5u.com熱心網友回復:
當模式不匹配任何內容時,這不是錯誤。如果要檢查模式是否與任何檔案不匹配,則應檢查files引數的長度。如果為零,則沒有匹配項,您可以采取相應措施。
if (err) {
console.error('An error occurred!', e);
} else if (files.length === 0) {
console.log('File Not Found !');
} else {
files.forEach(…)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485995.html
上一篇:element.join不是函式
