The requirements are as follows:
There are about 40 M of files in the entire directory, and there are countless files. As time goes by, I can't remember which file the string is in, so. The powerful, blinding Node.js made a brilliant debut.
There is no difference between installing Node.js in Windows and installing ordinary software. After installing, open the shortcut to Node.js, or directly cmd, you know.
Create findString.js
The code copy is as follows:
var path = require("path");
var fs = require("fs");
var filePath = process.argv[2];
var lookingForString = process.argv[3];
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");
}
Two parameters: the first parameter is "folder name" and the second parameter is "the string you are looking for"
As shown in the picture:
Print out the file path, finish the work. The speed is really fierce, blinding the eyes. . . If you use Java full text search, you will be miserable...