Created
May 6, 2014 03:26
-
-
Save SteveBenner/3dc01628965fa9dd3ce1 to your computer and use it in GitHub Desktop.
Function which returns a given filename or the incremented version thereof
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
def fname_incr(fname) | |
!File.exists?(fname) ? | |
fname : | |
/([0-9]+)\./.match(fname) ? | |
fname_incr(fname.sub(/([0-9]+)/) { |m| (m.to_i+1).to_s }) : | |
fname_incr(fname.sub(/[^\.]*/) { |m| m+'1' }) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment