zoukankan      html  css  js  c++  java
  • node搜索文件夹下的指定内容

    node搜索文件夹下的指定内容

    执行效果

    findString.js

    var path = require("path");
    var fs = require("fs");
    
    var filePath = "./src";
    var lookingForString = "add";
    
    findString(filePath, lookingForString);
    
    function findString(filePath, lookingForString) {
      recursiveReadFile(filePath);
    
      function recursiveReadFile(fileName) {
        if (!fs.existsSync(fileName)) return;
        if (isFile(fileName)) {
          check(fileName);
        }
        if (isDirectory(fileName)) {
          var files = fs.readdirSync(fileName);
          files.forEach(function(val, key) {
            var temp = path.join(fileName, val);
            if (isDirectory(temp)) recursiveReadFile(temp);
            if (isFile(temp)) check(temp);
          });
        }
      }
      function check(fileName) {
        var data = readFile(fileName);
        var exc = new RegExp(lookingForString);
        if (exc.test(data)) console.log(fileName);
      }
      function isDirectory(fileName) {
        if (fs.existsSync(fileName)) return fs.statSync(fileName).isDirectory();
      }
      function isFile(fileName) {
        if (fs.existsSync(fileName)) return fs.statSync(fileName).isFile();
      }
      function readFile(fileName) {
        if (fs.existsSync(fileName)) return fs.readFileSync(fileName, "utf-8");
      }
    }
    
    
    愿以往所学皆有所获
  • 相关阅读:
    zkw费用流
    luogu5212/bzoj2555 substring(后缀自动机+动态树)
    后缀数据结构模板2
    后缀数据结构模板1
    通用动态树(Link-Cut Tree)模板
    上下界网络流总结
    多项式多点求值
    拉格朗日反演
    多项式板子·新
    luogu2387 [NOI2014]魔法森林
  • 原文地址:https://www.cnblogs.com/Azune/p/14378216.html
Copyright © 2011-2022 走看看