Skip to content

Instantly share code, notes, and snippets.

@arjaneising
Forked from derekkraan/shift.rb
Created November 26, 2012 22:10
Show Gist options
  • Select an option

  • Save arjaneising/4150990 to your computer and use it in GitHub Desktop.

Select an option

Save arjaneising/4150990 to your computer and use it in GitHub Desktop.

Revisions

  1. @derekkraan derekkraan revised this gist Nov 26, 2012. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion shift.rb
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,15 @@ def new_filename(num, filename)
    end

    files = Dir['*']
    shift = ARGV.first
    shift = false
    dry_run = false
    ARGV.each do |arg|
    dry_run = true if arg == '-n'
    shift = $& if arg.to_s =~ /^[0-9]{1,4}$/
    end
    unless shift
    puts 'invalid parameter; exiting'
    exit 1
    end
    puts 'DRY RUN' if dry_run

  2. @invalid-email-address Anonymous created this gist Nov 26, 2012.
    37 changes: 37 additions & 0 deletions shift.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    def new_filename(num, filename)
    if num.to_i < 9
    "_0#{num.to_i+1}#{filename}"
    elsif num.to_i < 99
    "_#{num.to_i+1}#{filename}"
    else
    "#{num.to_i+1}#{filename}"
    end
    end

    files = Dir['*']
    shift = ARGV.first
    dry_run = false
    ARGV.each do |arg|
    dry_run = true if arg == '-n'
    end
    puts 'DRY RUN' if dry_run

    LESS_HUNDRED = /^_([0-9]{2})(.*)/
    HUNDRED_PLUS = /^([1-9][0-9]{2})(.*)/

    queued_moves = []
    [LESS_HUNDRED, HUNDRED_PLUS].each do |regex|
    files.select { |file| regex =~ file }.each do |file|
    regex =~ file
    if $1.to_i > shift.to_i
    puts "renaming `#{file}` to `#{new_filename($1, $2)}`"
    File.rename file, "__temp_#{$1}" unless dry_run
    queued_moves << ["__temp_#{$1}", new_filename($1, $2)]
    end
    end
    end
    unless dry_run
    queued_moves.each do |move|
    File.rename move[0], move[1]
    end
    end