Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function incrementFilename(name) {
- if (!name) {
- return '';
- }
- var matches = /^((.*) \()(-?[0-9]+)(\)(\.[a-zA-Z_0-9]+)?)$/ig.exec(name);
- if (matches && matches.length === 6) {
- // The file name already has an incremented number in the name (e.g. 'file (1).png' or 'file (1)')
- return matches[1] + (parseInt(matches[3]) + 1) + matches[4];
- }
- matches = /^(.*)(\.[a-zA-Z_0-9]+)$/ig.exec(name);
- if (matches && matches.length === 3) {
- // The file name has not yet an increment, but has an extension (e.g. 'file.png')
- return matches[1] + ' (1)' + matches[2];
- }
- // The name has no increment and no extension
- return name + ' (1)';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement