Skip to content

Instantly share code, notes, and snippets.

@muratcakmak
Last active December 27, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratcakmak/83d46f0542057f0fd4334269a1f3564c to your computer and use it in GitHub Desktop.
Save muratcakmak/83d46f0542057f0fd4334269a1f3564c to your computer and use it in GitHub Desktop.
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 1.
const fs = reqiure("fs");
(async () => {
let files = [];
let lineCount = 0;
const directoryPath = ".";
const fileType = "csv";
try {
const fileNames = fs.readdirSync(directoryPath);
fileNames.forEach((fileName) => {
const lastThree = fileName.substring(fileName.length - fileType.length);
if (lastThree === fileType) {
files.push(fileName);
}
});
} catch (e) {
console.error({ e });
}
console.log({ files });
fs.writeFile("merged.csv", "", function(err) {
if (err) throw err;
console.log("File is created successfully.");
});
files.forEach((filePath) => {
const csvString = fs.readFileSync(filePath, "utf8");
fs.appendFile("merged.csv", csvString, (err) => {
if (err) throw err;
console.log("File is merged successfully.");
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment