My previous colleague wrote a tool, but there was a bug, that is, after replacing the file, the format of the original file became utf8 BOM. This kind of XML with BOM may not be read on Mac, so I need to write a tool to deal with it.
In fact, the idea is relatively simple. First, iterate through the directory, then read the directory, remove the first three bytes of the file, and save it as a file in utf-8 format. Just enter the code:)
The code copy is as follows:
var fs = require('fs');
var path = "target path...";
function readDirectory(dirPath) {
if (fs.existsSync(dirPath)) {
var files = fs.readdirSync(dirPath);
files.forEach(function(file) {
var filePath = dirPath + "/" + file;
var stats = fs.statSync(filePath);
if (stats.isDirectory()) {
console.log('/n read directory: /n', filePath, "/n");
readDirectory(filePath);
} else if (stats.isFile()) {
var buff = fs.readFileSync(filePath);
if (buff[0].toString(16).toLowerCase() == "ef" && buff[1].toString(16).toLowerCase() == "bb" && buff[2].toString(16).toLowerCase() == "bf") {
//EF BB BF 239 187 191
console.log('/discover BOM file:', filePath, "/n");
buff = buff.slice(3);
fs.writeFile(filePath, buff.toString(), "utf8");
}
}
});
} else {
console.log('Not Found Path : ', dirPath);
}
}
readDirectory(path);