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");
      }
    }
    
    
    愿以往所学皆有所获
  • 相关阅读:
    分清函数指针和指针函数_
    常量指针与指针常量的区别
    Oracle11g数据库在Win系统下的安装
    MySQL-python模块
    堡垒机(paramiko)
    paramiko模块
    线程池
    线程、进程和协程
    Twisted
    SocketServer模块
  • 原文地址:https://www.cnblogs.com/Azune/p/14378216.html
Copyright © 2011-2022 走看看