Last active
February 19, 2019 07:20
-
-
Save titovanton/1e6f9d5185629730b045c1e24a8786f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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