Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This code assumes all files are in the /input folder.
- // The formatted files will be in the /output folder
- // Take care of these two things:
- // 1) The code loads each file in memory before transforming it - be sure you've got enough RAM to run it
- // 2) The code may break some code if it fails to format it
- var INPUT_DIR = "input";
- var OUTPUT_DIR = "output";
- var fs = require("fs");
- fs.readdir(INPUT_DIR, function (err, fileNames) {
- if (err) {
- console.log("There is no directory called " + INPUT_DIR);
- throw err;
- }
- fileNames.forEach(function (fileName, index) {
- var fullPath = INPUT_DIR + "/" + fileName;
- fs.readFile(fullPath, "utf-8", function (err, fileContents) {
- if (err) {
- console.log("There was a trouble reading file " + fullPath);
- throw err;
- }
- console.log("Processing file " + fullPath);
- // Remove many consecutive new lines and spaces
- fileContents = fileContents.replace(/(\r\n\s*)+/g, "\r\n");
- var outputPath = OUTPUT_DIR + "/" + fileName;
- fs.writeFile(outputPath, fileContents, function (err) {
- if (err) {
- console.log("There was a trouble writing file " + fullPath);
- throw err;
- }
- console.log("File " + outputPath + " saved");
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement