Skip to content

Instantly share code, notes, and snippets.

@nimit2801
Created June 1, 2021 20:03
Show Gist options
  • Save nimit2801/0e0238c7fa4ae72fbb2cc1ad8100891b to your computer and use it in GitHub Desktop.
Save nimit2801/0e0238c7fa4ae72fbb2cc1ad8100891b to your computer and use it in GitHub Desktop.
This gist help you to seperate files in your folder and copy paste them according to there extensions :D
const fs = require('fs');
const path = require('path');
var copyFile = (file, dir2) => {
var f = path.basename(file);
var source = fs.createReadStream(file);
var dest = fs.createWriteStream(path.resolve(dir2, f));
source.pipe(dest);
source.on('end', function () {
console.log('Succesfully copied');
});
source.on('error', function (err) {
console.log(err);
});
};
const logic = () => {
let fileNames = fs.readdirSync('./');
console.log('number of files in the the directory: ' + fileNames.length);
fileNames.forEach((fileName) => {
console.log(fileName);
});
console.log('Files names ending with .h');
let i = 0;
fileNames.forEach((fileName) => {
if (fileName.endsWith('.h')) {
i = i + 1;
console.log(fileName);
copyFile(`./${fileName}`, './headers');
}
});
// if you want to copy paste some other type of file
console.log('Number of files .h files in the directory: ' + i);
i = 0;
console.log('Files names ending with .cpp');
fileNames.forEach((fileName) => {
if (fileName.endsWith('.cpp')) {
i = i + 1;
console.log(fileName);
copyFile(`./${fileName}`, './source_files');
}
});
console.log('Number of files .cpp files in the directory: ' + i);
};
logic();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment