Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let _getError = () => new Error(`Usage: node ${process.argv[1]} [path to folder] [unwanted name part]`);
- /**
- * @type {String}
- *
- * Pass the folder path or return the current folder path.
- */
- let folderPath;
- /**
- * @type {String}
- *
- * Pass the unwanted name of the folder you want to rename. If invalid will throw an error.
- */
- let unwantedFolderName;
- if (3 >= process.argv.length <= 4) {
- if (process.argv.length === 3) {
- folderPath = '.';
- unwantedFolderName = process.argv[2];
- } else {
- folderPath = process.argv[2];
- unwantedFolderName = process.argv[3];
- }
- } else {
- throw _getError();
- }
- const fs = require('fs');
- /**
- *
- * @param path
- *
- * Return a array of folder(s) name(s).
- */
- const dirs = path => fs.readdirSync(path).filter(file => fs.statSync(path + "/" + file).isDirectory());
- let foldersNames = dirs(folderPath);
- if(!unwantedFolderName) {
- throw _getError();
- }
- foldersNames.forEach((item) => {
- let oldFolder = folderPath + item
- , newFolder = folderPath + item.replace(unwantedFolderName, '');
- fs.rename(oldFolder, newFolder, (err) => {
- if (err) throw err;
- console.log(`Rename completed: "${oldFolder}" to "${newFolder}";`);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement