Mon collègue précédent a écrit un outil, mais il y avait un bogue, c'est-à-dire après avoir remplacé le fichier, le format du fichier d'origine est devenu UTF8 BOM. Ce type de XML avec BOM peut ne pas être lu sur Mac, j'ai donc besoin d'écrire un outil pour y faire face.
En fait, l'idée est relativement simple. Tout d'abord, parcourez le répertoire, puis lisez le répertoire, supprimez les trois premiers octets du fichier et enregistrez-le en tant que fichier au format UTF-8. Entrez simplement le code :)
La copie de code est la suivante:
var fs = require ('fs');
var path = "Target Path ...";
fonction ReadDirectory (dirpath) {
if (fs.existSync (dirpath)) {
var files = fs.readDiRSYnc (dirpath);
files.ForEach (fonction (fichier) {
var filepath = dirpath + "/" + fichier;
var stats = fsatSync (filepath);
if (stats.isdirectory ()) {
console.log ('/ n read répertoire: / 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");
}
}
});
} autre {
Console.log («Path introuvable:», Dirpath);
}
}
ReadDirectory (chemin);