From 8cf3c29008e1ac5df0f64a9480fde2de641791a8 Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Thu, 18 Aug 2022 18:54:16 +0200 Subject: [PATCH] Add cleanuptxtfile.js For cleaning up text file that has been created by copying foobar2k songs and pasting into notepad. (basically just removing '[]' and everything in between :)) --- cleanuptxtfile.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 cleanuptxtfile.js diff --git a/cleanuptxtfile.js b/cleanuptxtfile.js new file mode 100644 index 0000000..015dd0d --- /dev/null +++ b/cleanuptxtfile.js @@ -0,0 +1,44 @@ +// Clean up txt file that is created by copying foobar2k songs and pasting into notepad. (Basically just removing '[]' and everything in between :)) +// First arg is path to file + +const fs = require('fs'); + + +function argHandler(args){ + let pathToTextFile = ""; + switch (args[2]) { + case "--help": + case "-h": + case undefined: + printHelp(); + break; + default: + pathToTextFile = args[2]; + break; + } + return pathToTextFile; +} + +function printHelp(){ + const helpText = `Usage:\n\tnode cleanuptxtfile.js \n`; + process.stdout.write(helpText); + process.exit(0); +} + +function fileNotFound(pathToTextFile) { + console.log(`FILE NOT FOUND: ${pathToTextFile}\n`) + process.exit(404) +} + +function cleanFile(pathToTextFile) { + let file = fs.readFileSync(pathToTextFile).toString(); + const regex = /(\[)(.*)(\])/gm + file = file.replace(regex, ""); + fs.writeFileSync(`CLEANED.txt`, file); +} + +let pathToTextFile = argHandler(process.argv); + +fs.existsSync(pathToTextFile) ? cleanFile(pathToTextFile) : fileNotFound(pathToTextFile) + +console.log(process.argv[2]) \ No newline at end of file