Advertisement
Guest User

Untitled

a guest
May 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. function incrementFilename(name) {
  2. if (!name) {
  3. return '';
  4. }
  5.  
  6. var matches = /^((.*) \()(-?[0-9]+)(\)(\.[a-zA-Z_0-9]+)?)$/ig.exec(name);
  7.  
  8. if (matches && matches.length === 6) {
  9. // The file name already has an incremented number in the name (e.g. 'file (1).png' or 'file (1)')
  10. return matches[1] + (parseInt(matches[3]) + 1) + matches[4];
  11. }
  12.  
  13. matches = /^(.*)(\.[a-zA-Z_0-9]+)$/ig.exec(name);
  14.  
  15. if (matches && matches.length === 3) {
  16. // The file name has not yet an increment, but has an extension (e.g. 'file.png')
  17. return matches[1] + ' (1)' + matches[2];
  18. }
  19.  
  20. // The name has no increment and no extension
  21. return name + ' (1)';
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement