如何允许仅从node.js目录中读取前3个文件

olhwl3o2  于 2022-10-22  发布在  Node.js
关注(0)|答案(2)|浏览(153)

以下是代码

const fs = require('fs');
const xml2js = require('xml2js');
const log = console.log;
const path = require( "path" );
const folderPath = 'Data';
const folder = folderPath;
let counter = 0;

 fs.readdirSync(folder).forEach(file => {
    const extname = path.extname(file);
    const filename = path.basename(file, extname);
    const absolutePath = path.resolve(folder, file);

    const parser = new xml2js.Parser({
        mergeAttrs: true,
        explicitArray: false
    });

counter++;

fs.readFile(absolutePath, (err, data) => {

        parser.parseString(data, (err, result) => {
            var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
            MongoClient.connect(url, function (err, db) {
                 if (err) throw err;
var dbo = db.db("mydb");
                 dbo.collection("customers").insertOne(result, function (err, res) {
                    if (err) throw err;
console.log("XML Uploaded Succesfully : " + absolutePath);
                    db.close();
                });  
            });
        });
    });  
});

console.log("Number of XML inserted: " + counter);

=============================================================
上面的代码运行良好,它可以读取数据文件夹中存在的所有文件,我想限制这一点,即假设我在数据文件夹中有10个文件,但只想读取前5个文件,在读取前5个文件后,我希望它移动到另一个文件夹中,并将其设置为“已读完”

ttisahbt

ttisahbt1#

我希望它能做你想做的事
在forEach方法中使用索引,它将为您提供数组中的文件位置
根据Do One If条件,您可以执行任务

const fs = require('fs');
const xml2js = require('xml2js');
const log = console.log;
const path = require("path");
const folderPath = 'Data';
const folder = folderPath;

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("mydb");

    fs.readdirSync(folder).forEach((file, index) => {

        if (index == 3) { //replace value 3 as per ur condition 

          //also u can pass the value dynamically here

          const extname = path.extname(file);
          const filename = path.basename(file, extname);
          const absolutePath = path.resolve(folder, file);

          const parser = new xml2js.Parser({
            mergeAttrs: true,
            explicitArray: false
          });

          fs.readFile(absolutePath, (err, data) => {

            parser.parseString(data, (err, result) => {

              dbo.collection("customers").insertOne(result, function(err, res) {
                if (err) throw err;
                console.log("XML Uploaded Succesfully : " + absolutePath);
                db.close();
              });
            });
          });
        });
    });

}
else {

  // do for remaining files here which can read from other directory or whatever u want
}
mwngjboj

mwngjboj2#

const fs = require('fs');
const ex = require('fs-extra');

const xml2js = require('xml2js');
const log = console.log;
const path = require( "path" );
const folderPath = 'Data';
const folder = folderPath;
let counter = 0;

 fs.readdirSync(folder).slice(0,3).forEach((file,index) => {

    const extname = path.extname(file);
    const filename = path.basename(file, extname);
    const absolutePath = path.resolve(folder, file);
    const parser = new xml2js.Parser({
        mergeAttrs: true,
        explicitArray: false
    });

counter++;

fs.readFile(absolutePath, (err, data) => {
        parser.parseString(data, (err, result) => {
            var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
            MongoClient.connect(url, function (err, db) {
                 if (err) throw err;
var dbo = db.db("mydb");
                 dbo.collection("customers").insertOne(result, function (err, res) {
                    if (err) throw err;
const dest = "readingDone/" + filename + extname;
 ex.move(absolutePath, dest, (err) => {
  if (err) return console.log(err);
  console.log('File successfully moved!!');
}); 
//console.log(extname);
console.log("XML Uploaded Succesfully : " + absolutePath);

                    db.close();
                });  
            });
        });
    });  

});

console.log("Number of XML inserted: " + counter);

相关问题