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");
      }
    }
    
    
    愿以往所学皆有所获
  • 相关阅读:
    Python--day72--ajax简介
    Python基础1 介绍、基本语法
    10-Python-字符编码
    09-Python-集合
    07-Python-字符串操作
    99-Python-深浅拷贝
    06-Python-字典
    05-Python-判断语句
    04-Python-元组
    03-Python-操作列表
  • 原文地址:https://www.cnblogs.com/Azune/p/14378216.html
Copyright © 2011-2022 走看看