Class: Renamer

Inherits:
Object
  • Object
show all
Defined in:
lib/renamer.rb,
lib/command_line.rb

Overview

Class Renamer handles files renaming

Defined Under Namespace

Classes: CommandLine

Constant Summary collapse

EXISTS =
_("%s: %s file already exists!")

Instance Method Summary collapse

Constructor Details

#initialize(filenames, recursive = false, overwrite = false) ⇒ Renamer

Create a new Renamer object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/renamer.rb', line 30

def initialize(filenames, recursive = false, overwrite = false)
  # Whether user wants to overwrite existing files or not
  @overwrite = overwrite

  # User wants to process files recursively
  if recursive
    @filenames = Array.new
    filenames.each do |f|
      if File.directory?(f)
        @filenames << Dir.glob("#{f}/**/*")
      else
        @filenames << f
      end
    end
    # In recursive mode we don't want to process directory names
    @filenames.flatten!
    @filenames.delete_if { |f| File.directory?(f) }
    # User just wants to process given files
  else
    @filenames = filenames
  end
end

Instance Method Details

#basename(bn) ⇒ Object

Rename files using a basename and a four digits number



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/renamer.rb', line 157

def basename(bn)
  proc_basename = Proc.new do |file, basename|
    i = "0000"
    file =~ /.*?\.(.*)$/
    if $1 then
      while File.exist?("#{basename}#{i}.#{$1}")
        i = i.to_i + 1
        i = sprintf("%04d", i.to_i)
      end
      new = basename + i + "." + $1
    else
      while File.exist?("#{basename}#{i}")
        i = i.to_i + 1
        i = sprintf("%04d", i.to_i)
      end
      new = basename + i
    end
    rename(file, new)
  end
  run(proc_basename, bn)
end

#capitalizeObject

Capitalize filenames



132
133
134
135
136
137
138
# File 'lib/renamer.rb', line 132

def capitalize
  proc_capitalize = Proc.new do |file|
    new = file.capitalize
    rename_fs(file, new)
  end
  run(proc_capitalize)
end

#capitalize_wordsObject

Capitalize each word



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/renamer.rb', line 141

def capitalize_words
  proc_wcapitalize = Proc.new do |file|
    # We start with a clean downcase name
    new = file.downcase
    # Processing each words
    new.gsub!(/(?:^|[\s_\b\.])\w/) { |el| el.upcase }
    # extension has to be downcase
    new.gsub!(/(\.\w+$)/) { |el| el.downcase }
    # dotfiles have to be downcase
    new.downcase! if new =~ /^\..+/
    rename_fs(file, new)
  end
  run(proc_wcapitalize)
end

#current_date(time) ⇒ Object

Add date (using time arg) to the beginning of the filename



237
238
239
240
241
242
243
# File 'lib/renamer.rb', line 237

def current_date(time)
  proc_current_date = Proc.new do |file, date|
    new = [date, file].join('_')
    rename(file, new)
  end
  run(proc_current_date, time.strftime("%Y-%m-%d"))
end

#current_datetime(datetime) ⇒ Object

Add datetime (using time arg) to the beginning of the filename



246
247
248
249
250
251
252
# File 'lib/renamer.rb', line 246

def current_datetime(datetime)
  proc_current_datetime = Proc.new do |file, datetime|
    new = [datetime, file].join('_')
    rename(file, new)
  end
  run(proc_current_datetime, datetime.strftime("%Y-%m-%d_%H-%M"))
end

#downcaseObject

Downcase filenames



54
55
56
57
58
59
60
# File 'lib/renamer.rb', line 54

def downcase
  proc_dc = Proc.new do |file|
    new = file.downcase
    rename_fs(file, new)
  end
  run(proc_dc)
end

#downcase_extObject

Downcase filename extensions



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/renamer.rb', line 72

def downcase_ext
  proc_dc_ext = Proc.new do |file|
    # We don't want to process dotfiles
    unless File.basename(file) =~ /^\./
      # We want to grep the last extension
      name = file.split('.')
      if name.size > 1 # Ensure that there's an extension
        # Now we downcase it
        ext =  name.last.downcase
        # Building the new name
        name = name[0...-1].join(".")
        new = name + '.' + ext
        # Renaming
        rename_fs(file, new)
      end
    end
  end
  run(proc_dc_ext)
end

#ext(e) ⇒ Object

Change file extensions



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/renamer.rb', line 180

def ext(e)
  proc_ext = Proc.new do |file, extension|
    # We don't want to process dotfiles
    unless File.basename(file) =~ /^\./
      # We want to remove the last extension
      name = file.split(".")
      name = name[0...-1].join(".") if name.size > 1
      name = name.to_s
      # Adding the new extension
      new = (extension == "" ? name : name + "." + extension)
      rename(file, new)
    end
  end
  run(proc_ext, e)
end

#space_to_underscoreObject

Replace spaces in filenames by underscores



114
115
116
117
118
119
120
# File 'lib/renamer.rb', line 114

def space_to_underscore
  proc_s2u = Proc.new do |file|
    new = file.gsub(/\s/, '_')
    rename(file, new)
  end
  run(proc_s2u)
end

#trim_first(n) ⇒ Object

Trim first n characters



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/renamer.rb', line 197

def trim_first(n)
  proc_trim_first = Proc.new do |file, n|
    # Grabbing name and ext
    elements = file.split(".")
    if elements.size < 2
      name = elements.first
      ext = nil
    else
      name = elements[0...-1].join('.')
      ext = elements[-1, 1].first
    end

    # trimming the n first characters
    new = [name[n..-1], ext].compact.join('.')
    rename(file, new)
  end
  run(proc_trim_first, n)
end

#trim_last(n) ⇒ Object

Trim last n characters



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/renamer.rb', line 217

def trim_last(n)
  proc_trim_last = Proc.new do |file, n|
    # Grabbing name and ext
    elements = file.split(".")
    if elements.size < 2
      name = elements.first
      ext = nil
    else
      name = elements[0...-1].join('.')
      ext = elements[-1, 1].first
    end

    # trimming the n last characters
    new = [name[0, name.length - n], ext].compact.join('.')
    rename(file, new)
  end
  run(proc_trim_last, n)
end

#underscore_to_spaceObject

Convert underscores to spaces



123
124
125
126
127
128
129
# File 'lib/renamer.rb', line 123

def underscore_to_space
  proc_u2s = Proc.new do |file|
    new = file.gsub(/_/, ' ')
    rename(file, new)
  end
  run(proc_u2s)
end

#upcaseObject

Upcase filenames



63
64
65
66
67
68
69
# File 'lib/renamer.rb', line 63

def upcase
  proc_uc = Proc.new do |file|
    new = file.upcase
    rename_fs(file, new)
  end
  run(proc_uc)
end

#upcase_extObject

Upcase filename extensions



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/renamer.rb', line 93

def upcase_ext
  proc_uc_ext = Proc.new do |file|
    # We don't want to process dotfiles
    unless File.basename(file) =~ /^\./
      # We want to grep the last extension
      name = file.split('.')
      if name.size > 1 # Ensure that there's an extension
        # Now we upcase it
        ext =  name.last.upcase
        # Building the new name
        name = name[0...-1].join(".")
        new = name + '.' + ext
        # Renaming
        rename_fs(file, new)
      end
    end
  end
  run(proc_uc_ext)
end