Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Created May 6, 2014 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveBenner/3dc01628965fa9dd3ce1 to your computer and use it in GitHub Desktop.
Save SteveBenner/3dc01628965fa9dd3ce1 to your computer and use it in GitHub Desktop.
Function which returns a given filename or the incremented version thereof
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