js fs test

var fs=require('fs');

const lineReader = require('line-reader');

var reg = /<a href=.*?(?=('>))/g;

var reg2 = /(?<=('>))[0-9a-z_A-Z]*?(?=</a)/g;
/*
lineReader.eachLine('index.html', function(line) {

    var mat =null
    do {
        mat = reg.exec(line)
        if(mat) {
            console.log(mat[0])
        }
    }while(mat)

});
*/


/*
lineReader.eachLine('index.html', (line)=>{    

    console.log(line.replace(reg, "$&.html"))
})
*/


function lsAllFiles(path) {
  try {
    const res = fs.readdirSync(path);
    res.forEach((file) => {
      const abs = path + "/" + file;
      const stats = fs.statSync(abs);
      if (stats.isFile()) {
        lineReader.eachLine(abs, (line)=>{
            let nl = line.replace(reg2,"$&.html")
            fs.appendFileSync(abs+"zzz", nl+"
");
        })
          //fs.rename(abs, abs+".html", function(err) {if ( err ) console.log('ERROR: ' + err);});
      } else if (stats.isDirectory()) {
        lsAllFiles(abs);
      } else {
        console.log(`Warn! Item type is not file nor directory: ${abs}`);
      }
    });
  } catch (e) {
    console.log(e);
  }
}

lsAllFiles("./files/")
原文地址:https://www.cnblogs.com/Searchor/p/14045297.html