Skip to content

Instantly share code, notes, and snippets.

@titovanton
Last active February 19, 2019 07:20
Show Gist options
  • Save titovanton/1e6f9d5185629730b045c1e24a8786f1 to your computer and use it in GitHub Desktop.
Save titovanton/1e6f9d5185629730b045c1e24a8786f1 to your computer and use it in GitHub Desktop.
// TODO: needs to be done
const renameExists = () => {
/** Auto rename a file if the same name exists in a destination folder
* example: file.txt, file(1).txt, file(2).txt
*/
while (exists) {
let filename = exists.title;
let copyNumber = 1;
let ext = '';
// extract extantion
let matches = filename.match(/^(.+)([.][^.]+)$/);
if (matches) {
filename = matches[1];
ext = matches[2];
}
// extract copy number
matches = filename.match(/^(.+)\((\d+)\)$/);
if (matches) {
filename = matches[1];
copyNumber = parseInt(matches[2]) + 1;
}
// copyNumber = parseInt(copyNumber) + 1;
nodeFrom.title = `${filename}(${copyNumber})${ext}`;
exists = FilesTree.findOne({
parent: nodeTo._id,
title: nodeFrom.title,
folder: nodeFrom.folder
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment