Renamer 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,9 +38,7 @@
38
38
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39
39
  =end
40
40
 
41
- #############
42
- # Libraries #
43
- #############
41
+ # Libraries
44
42
  begin
45
43
  require 'rubygems'
46
44
  rescue LoadError
@@ -53,93 +51,12 @@ require 'tempfile'
53
51
 
54
52
  require 'const'
55
53
  require 'renamer'
54
+ require 'command_line'
56
55
 
57
- ########################
58
- # Initiliazing GetText #
59
- ########################
56
+ # Initiliazing GetText
60
57
  include GetText
61
- # Searching the path to locale directory
62
58
  p = File.join(File.dirname(__FILE__), "/../locale/")
63
59
  bindtextdomain("renamer", p)
64
60
 
65
- ########################
66
- # Command Line Options #
67
- ########################
68
- action, arg, $VERBOSE = nil
69
- recursive, overwrite = false
70
-
71
- opts = OptionParser.new
72
-
73
- opts.banner = _("Usage: %s [options] FILENAMES") % File.basename($0)
74
- opts.separator ""
75
- opts.separator _("Specific options:")
76
-
77
- opts.on("-d", "--downcase", _("Downcase FILENAMES")) { action = :downcase }
78
- opts.on("-u", "--upcase", _("Upcase FILENAMES")) { action = :upcase }
79
- opts.on("-c", "--capitalize", _("Capitalize FILENAMES")) { action = :capitalize }
80
- opts.on("-w", "--word", _("Capitalize each words in FILENAMES")) { action = :word }
81
- opts.on("--downcase_ext", _("Downcase FILENAMES extensions")) { action = :downcase_ext }
82
- opts.on("--upcase_ext", _("Upcase FILENAMES extensions")) { action = :upcase_ext }
83
- opts.on("-s", "--space", _("Replace spaces by underscores in FILENAMES")) { action = :space }
84
- opts.on("-U", "--underscore", _("Replace underscores by spaces in FILENAMES")) { action = :underscore }
85
- opts.on("--date", _("Add current date to the beginning of FILENAMES")) { action = :date }
86
- opts.on("--datetime", _("Add current datetime to the beginning of FILENAMES")) { action = :datetime }
87
- opts.on("-b", _("--basename BASENAME"), _("Rename FILENAMES using the specified"), _("BASENAME followed by an incremental number")) { |bn| action = :basename; arg = bn }
88
- opts.on("-e", _("--ext NEW"), _("Rename FILENAMES from their actual extension"), _("to the NEW one")) { |new| action = :ext; arg = new }
89
- opts.on("-f", "--trim-first n", _("Rename FILENAMES by trimming first n characters")) { |n| action = :trim_first; arg = n }
90
- opts.on("-l", "--trim-last n", _("Rename FILENAMES by trimming last n characters"), _("to the NEW one")) { |n| action = :trim_last; arg = n }
91
- opts.on("-r", "--recursive", _("Run %s recursively across directories") % Const::NAME) { recursive = true }
92
- opts.on("-o", "--overwrite", _("Overwrite existing files if needed") % Const::NAME) { overwrite = true }
93
-
94
- opts.separator ""
95
- opts.separator _("Common options:")
96
-
97
- opts.on_tail("-h", "--help", _("Show this help")) { puts opts.to_s; exit(0) }
98
- opts.on("-v", "--version", _("Show %s version") % Const::NAME) { puts Const::NAME + " v" + Const::VER; exit(0) }
99
- opts.on("-L", "--license", _("Show some information about %s license") % Const::NAME) { puts Const::LICENSE; exit(0) }
100
- opts.on("-V", "--verbose", _("Run %s in verbose mode (quiet by default)") % Const::NAME) { $VERBOSE = true }
101
-
102
- ###################
103
- # Parsing options #
104
- ###################
105
- if ARGV.empty?
106
- puts opts.to_s
107
- exit(1)
108
- end
109
- filenames = opts.parse(ARGV)
110
-
111
- ####################
112
- # Processing files #
113
- ####################
114
- r = Renamer.new(filenames, recursive, overwrite)
115
-
116
- case action
117
- when :downcase
118
- r.downcase
119
- when :upcase
120
- r.upcase
121
- when :downcase_ext
122
- r.downcase_ext
123
- when :upcase_ext
124
- r.upcase_ext
125
- when :capitalize
126
- r.capitalize
127
- when :word
128
- r.capitalize_words
129
- when :space
130
- r.space_to_underscore
131
- when :underscore
132
- r.underscore_to_space
133
- when :basename
134
- r.basename(arg)
135
- when :ext
136
- r.ext(arg)
137
- when :trim_first
138
- r.trim_first(arg)
139
- when :trim_last
140
- r.trim_last(arg)
141
- when :date
142
- r.current_date(Time.now)
143
- when :datetime
144
- r.current_datetime(Time.now)
145
- end
61
+ # Command Line Handling and processing
62
+ Renamer::CommandLine.new
@@ -1,5 +1,10 @@
1
1
  = Renamer - The perfect tool to easily rename your files
2
2
 
3
+ === Renamer 0.5.1
4
+
5
+ 2008-11-04 Nicolas Cavigneaux <nico@bounga.org>
6
+
7
+ * Modularized command-line processing
3
8
 
4
9
  === Renamer 0.5.0
5
10
 
@@ -0,0 +1,99 @@
1
+ class Renamer
2
+ # Process command-line arguments
3
+ class CommandLine
4
+ attr_accessor :action, :arg, :recursive, :overwrite, :opts, :filenames
5
+
6
+ def initialize
7
+ $VERBOSE, self.recursive, self.overwrite = false
8
+ self.action, self.arg = nil
9
+
10
+ feed
11
+ parse
12
+ process
13
+ end
14
+
15
+ private
16
+
17
+ # Define available options
18
+ def feed
19
+ opts = OptionParser.new
20
+
21
+ opts.banner = _("Usage: %s [options] FILENAMES") % File.basename($0)
22
+ opts.separator ""
23
+ opts.separator _("Specific options:")
24
+
25
+ opts.on("-d", "--downcase", _("Downcase FILENAMES")) { self.action = :downcase }
26
+ opts.on("-u", "--upcase", _("Upcase FILENAMES")) { self.action = :upcase }
27
+ opts.on("-c", "--capitalize", _("Capitalize FILENAMES")) { self.action = :capitalize }
28
+ opts.on("-w", "--word", _("Capitalize each words in FILENAMES")) { self.action = :word }
29
+ opts.on("--downcase_ext", _("Downcase FILENAMES extensions")) { self.action = :downcase_ext }
30
+ opts.on("--upcase_ext", _("Upcase FILENAMES extensions")) { self.action = :upcase_ext }
31
+ opts.on("-s", "--space", _("Replace spaces by underscores in FILENAMES")) { self.action = :space }
32
+ opts.on("-U", "--underscore", _("Replace underscores by spaces in FILENAMES")) { self.action = :underscore }
33
+ opts.on("--date", _("Add current date to the beginning of FILENAMES")) { self.action = :date }
34
+ opts.on("--datetime", _("Add current datetime to the beginning of FILENAMES")) { self.action = :datetime }
35
+ opts.on("-b", _("--basename BASENAME"), _("Rename FILENAMES using the specified"), _("BASENAME followed by an incremental number")) { |bn| self.action = :basename; self.arg = bn }
36
+ opts.on("-e", _("--ext NEW"), _("Rename FILENAMES from their actual extension"), _("to the NEW one")) { |new| self.action = :ext; self.arg = new }
37
+ opts.on("-f", "--trim-first N", Integer, _("Rename FILENAMES by trimming first N characters")) { |n| self.action = :trim_first; self.arg = n }
38
+ opts.on("-l", "--trim-last N", Integer, _("Rename FILENAMES by trimming last N characters")) { |n| self.action = :trim_last; self.arg = n }
39
+ opts.on("-r", "--recursive", _("Run %s recursively across directories") % Const::NAME) { self.recursive = true }
40
+ opts.on("-o", "--overwrite", _("Overwrite existing files if needed") % Const::NAME) { self.overwrite = true }
41
+
42
+ opts.separator ""
43
+ opts.separator _("Common options:")
44
+
45
+ opts.on_tail("-h", "--help", _("Show this help")) { puts opts.to_s; exit(0) }
46
+ opts.on("-v", "--version", _("Show %s version") % Const::NAME) { puts Const::NAME + " v" + Const::VER; exit(0) }
47
+ opts.on("-L", "--license", _("Show some information about %s license") % Const::NAME) { puts Const::LICENSE; exit(0) }
48
+ opts.on("-V", "--verbose", _("Run %s in verbose mode (quiet by default)") % Const::NAME) { $VERBOSE = true }
49
+
50
+ self.opts = opts
51
+ end
52
+
53
+ # Parse arguments
54
+ def parse
55
+ if ARGV.empty?
56
+ puts self.opts.to_s
57
+ exit(1)
58
+ end
59
+
60
+ self.filenames = self.opts.parse(ARGV)
61
+ end
62
+
63
+ # Send the requested job to Renamer
64
+ def process
65
+ r = Renamer.new(self.filenames, self.recursive, self.overwrite)
66
+
67
+ case self.action
68
+ when :downcase
69
+ r.downcase
70
+ when :upcase
71
+ r.upcase
72
+ when :downcase_ext
73
+ r.downcase_ext
74
+ when :upcase_ext
75
+ r.upcase_ext
76
+ when :capitalize
77
+ r.capitalize
78
+ when :word
79
+ r.capitalize_words
80
+ when :space
81
+ r.space_to_underscore
82
+ when :underscore
83
+ r.underscore_to_space
84
+ when :basename
85
+ r.basename(self.arg)
86
+ when :ext
87
+ r.ext(self.arg)
88
+ when :trim_first
89
+ r.trim_first(self.arg)
90
+ when :trim_last
91
+ r.trim_last(self.arg)
92
+ when :date
93
+ r.current_date(Time.now)
94
+ when :datetime
95
+ r.current_datetime(Time.now)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -18,9 +18,9 @@
18
18
  =end
19
19
 
20
20
  module Const
21
- NAME = "Renamer"
22
- VER = "0.5.1"
23
- LICENSE = "
21
+ NAME = "Renamer"
22
+ VER = "0.5.2"
23
+ LICENSE = "
24
24
  Copyright (c) 2004-2008 by Nicolas Cavigneaux <nico@bounga.org>
25
25
  See COPIYNG for License detail.
26
26
 
@@ -17,285 +17,282 @@
17
17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
  =end
19
19
 
20
- ########################
21
- # Initiliazing GetText #
22
- ########################
20
+ # Initiliazing GetText
23
21
  include GetText
24
- # Searching the path to locale directory
25
22
  p = File.join(File.dirname(__FILE__), "/../locale/")
26
23
  bindtextdomain("renamer", p)
27
24
 
28
25
  # Class Renamer handles files renaming
29
26
  class Renamer
30
- EXISTS = _("%s: %s file already exists!")
27
+ EXISTS = _("%s: %s file already exists!")
31
28
 
32
- # Create a new Renamer object
33
- def initialize(filenames, recursive = false, overwrite = false)
34
- # Whether user wants to overwrite existing files or not
35
- @overwrite = overwrite
29
+ # Create a new Renamer object
30
+ def initialize(filenames, recursive = false, overwrite = false)
31
+ # Whether user wants to overwrite existing files or not
32
+ @overwrite = overwrite
36
33
 
37
- # User wants to process files recursively
38
- if recursive
39
- @filenames = Array.new
40
- filenames.each do |f|
41
- if File.directory?(f)
42
- @filenames << Dir.glob("#{f}/**/*")
43
- else
44
- @filenames << f
45
- end
46
- end
47
- # In recursive mode we don't want to process directory names
48
- @filenames.flatten!
49
- @filenames.delete_if { |f| File.directory?(f) }
50
- # User just wants to process given files
34
+ # User wants to process files recursively
35
+ if recursive
36
+ @filenames = Array.new
37
+ filenames.each do |f|
38
+ if File.directory?(f)
39
+ @filenames << Dir.glob("#{f}/**/*")
51
40
  else
52
- @filenames = filenames
41
+ @filenames << f
53
42
  end
43
+ end
44
+ # In recursive mode we don't want to process directory names
45
+ @filenames.flatten!
46
+ @filenames.delete_if { |f| File.directory?(f) }
47
+ # User just wants to process given files
48
+ else
49
+ @filenames = filenames
54
50
  end
51
+ end
55
52
 
56
- # Downcase filenames
57
- def downcase
58
- proc_dc = Proc.new do |file|
59
- new = file.downcase
60
- rename_fs(file, new)
61
- end
62
- run(proc_dc)
63
- end
64
-
65
- # Upcase filenames
66
- def upcase
67
- proc_uc = Proc.new do |file|
68
- new = file.upcase
69
- rename_fs(file, new)
70
- end
71
- run(proc_uc)
53
+ # Downcase filenames
54
+ def downcase
55
+ proc_dc = Proc.new do |file|
56
+ new = file.downcase
57
+ rename_fs(file, new)
72
58
  end
59
+ run(proc_dc)
60
+ end
73
61
 
74
- # Downcase filename extensions
75
- def downcase_ext
76
- proc_dc_ext = Proc.new do |file|
77
- # We don't want to process dotfiles
78
- unless File.basename(file) =~ /^\./
79
- # We want to grep the last extension
80
- name = file.split('.')
81
- if name.size > 1 # Ensure that there's an extension
82
- # Now we downcase it
83
- ext = name.last.downcase
84
- # Building the new name
85
- name = name[0...-1].join(".")
86
- new = name + '.' + ext
87
- # Renaming
88
- rename_fs(file, new)
89
- end
90
- end
91
- end
92
- run(proc_dc_ext)
62
+ # Upcase filenames
63
+ def upcase
64
+ proc_uc = Proc.new do |file|
65
+ new = file.upcase
66
+ rename_fs(file, new)
93
67
  end
68
+ run(proc_uc)
69
+ end
94
70
 
95
- # Upcase filename extensions
96
- def upcase_ext
97
- proc_uc_ext = Proc.new do |file|
98
- # We don't want to process dotfiles
99
- unless File.basename(file) =~ /^\./
100
- # We want to grep the last extension
101
- name = file.split('.')
102
- if name.size > 1 # Ensure that there's an extension
103
- # Now we upcase it
104
- ext = name.last.upcase
105
- # Building the new name
106
- name = name[0...-1].join(".")
107
- new = name + '.' + ext
108
- # Renaming
109
- rename_fs(file, new)
110
- end
111
- end
71
+ # Downcase filename extensions
72
+ def downcase_ext
73
+ proc_dc_ext = Proc.new do |file|
74
+ # We don't want to process dotfiles
75
+ unless File.basename(file) =~ /^\./
76
+ # We want to grep the last extension
77
+ name = file.split('.')
78
+ if name.size > 1 # Ensure that there's an extension
79
+ # Now we downcase it
80
+ ext = name.last.downcase
81
+ # Building the new name
82
+ name = name[0...-1].join(".")
83
+ new = name + '.' + ext
84
+ # Renaming
85
+ rename_fs(file, new)
112
86
  end
113
- run(proc_uc_ext)
87
+ end
114
88
  end
89
+ run(proc_dc_ext)
90
+ end
115
91
 
116
- # Replace spaces in filenames by underscores
117
- def space_to_underscore
118
- proc_s2u = Proc.new do |file|
119
- new = file.gsub(/\s/, '_')
120
- rename(file, new)
92
+ # Upcase filename extensions
93
+ def upcase_ext
94
+ proc_uc_ext = Proc.new do |file|
95
+ # We don't want to process dotfiles
96
+ unless File.basename(file) =~ /^\./
97
+ # We want to grep the last extension
98
+ name = file.split('.')
99
+ if name.size > 1 # Ensure that there's an extension
100
+ # Now we upcase it
101
+ ext = name.last.upcase
102
+ # Building the new name
103
+ name = name[0...-1].join(".")
104
+ new = name + '.' + ext
105
+ # Renaming
106
+ rename_fs(file, new)
121
107
  end
122
- run(proc_s2u)
108
+ end
123
109
  end
110
+ run(proc_uc_ext)
111
+ end
124
112
 
125
- # Convert underscores to spaces
126
- def underscore_to_space
127
- proc_u2s = Proc.new do |file|
128
- new = file.gsub(/_/, ' ')
129
- rename(file, new)
130
- end
131
- run(proc_u2s)
113
+ # Replace spaces in filenames by underscores
114
+ def space_to_underscore
115
+ proc_s2u = Proc.new do |file|
116
+ new = file.gsub(/\s/, '_')
117
+ rename(file, new)
132
118
  end
119
+ run(proc_s2u)
120
+ end
133
121
 
134
- # Capitalize filenames
135
- def capitalize
136
- proc_capitalize = Proc.new do |file|
137
- new = file.capitalize
138
- rename_fs(file, new)
139
- end
140
- run(proc_capitalize)
122
+ # Convert underscores to spaces
123
+ def underscore_to_space
124
+ proc_u2s = Proc.new do |file|
125
+ new = file.gsub(/_/, ' ')
126
+ rename(file, new)
141
127
  end
128
+ run(proc_u2s)
129
+ end
142
130
 
143
- # Capitalize each word
144
- def capitalize_words
145
- proc_wcapitalize = Proc.new do |file|
146
- # We start with a clean downcase name
147
- new = file.downcase
148
- # Processing each words
149
- new.gsub!(/(?:^|[\s_\b\.])\w/) { |el| el.upcase }
150
- # extension has to be downcase
151
- new.gsub!(/(\.\w+$)/) { |el| el.downcase }
152
- # dotfiles have to be downcase
153
- new.downcase! if new =~ /^\..+/
154
- rename_fs(file, new)
155
- end
156
- run(proc_wcapitalize)
131
+ # Capitalize filenames
132
+ def capitalize
133
+ proc_capitalize = Proc.new do |file|
134
+ new = file.capitalize
135
+ rename_fs(file, new)
157
136
  end
137
+ run(proc_capitalize)
138
+ end
158
139
 
159
- # Rename files using a basename and a four digits number
160
- def basename(bn)
161
- proc_basename = Proc.new do |file, basename|
162
- i = "0000"
163
- file =~ /.*?\.(.*)$/
164
- if $1 then
165
- while File.exist?("#{basename}#{i}.#{$1}")
166
- i = i.to_i + 1
167
- i = sprintf("%04d", i.to_i)
168
- end
169
- new = basename + i + "." + $1
170
- else
171
- while File.exist?("#{basename}#{i}")
172
- i = i.to_i + 1
173
- i = sprintf("%04d", i.to_i)
174
- end
175
- new = basename + i
176
- end
177
- rename(file, new)
178
- end
179
- run(proc_basename, bn)
140
+ # Capitalize each word
141
+ def capitalize_words
142
+ proc_wcapitalize = Proc.new do |file|
143
+ # We start with a clean downcase name
144
+ new = file.downcase
145
+ # Processing each words
146
+ new.gsub!(/(?:^|[\s_\b\.])\w/) { |el| el.upcase }
147
+ # extension has to be downcase
148
+ new.gsub!(/(\.\w+$)/) { |el| el.downcase }
149
+ # dotfiles have to be downcase
150
+ new.downcase! if new =~ /^\..+/
151
+ rename_fs(file, new)
180
152
  end
153
+ run(proc_wcapitalize)
154
+ end
181
155
 
182
- # Change file extensions
183
- def ext(e)
184
- proc_ext = Proc.new do |file, extension|
185
- # We don't want to process dotfiles
186
- unless File.basename(file) =~ /^\./
187
- # We want to remove the last extension
188
- name = file.split(".")
189
- name = name[0...-1].join(".") if name.size > 1
190
- name = name.to_s
191
- # Adding the new extension
192
- new = (extension == "" ? name : name + "." + extension)
193
- rename(file, new)
194
- end
156
+ # Rename files using a basename and a four digits number
157
+ def basename(bn)
158
+ proc_basename = Proc.new do |file, basename|
159
+ i = "0000"
160
+ file =~ /.*?\.(.*)$/
161
+ if $1 then
162
+ while File.exist?("#{basename}#{i}.#{$1}")
163
+ i = i.to_i + 1
164
+ i = sprintf("%04d", i.to_i)
195
165
  end
196
- run(proc_ext, e)
197
- end
198
-
199
- # Trim first n characters
200
- def trim_first(n)
201
- proc_trim_first = Proc.new do |file, n|
202
- # Grabbing name and ext
203
- elements = file.split(".")
204
- if elements.size < 2
205
- name = elements.first
206
- ext = nil
207
- else
208
- name = elements[0...-1].join('.')
209
- ext = elements[-1, 1].first
210
- end
211
-
212
- # trimming the n first characters
213
- new = [name[n..-1], ext].compact.join('.')
214
- rename(file, new)
166
+ new = basename + i + "." + $1
167
+ else
168
+ while File.exist?("#{basename}#{i}")
169
+ i = i.to_i + 1
170
+ i = sprintf("%04d", i.to_i)
215
171
  end
216
- run(proc_trim_first, n)
172
+ new = basename + i
173
+ end
174
+ rename(file, new)
217
175
  end
218
-
219
- # Trim last n characters
220
- def trim_last(n)
221
- proc_trim_last = Proc.new do |file, n|
222
- # Grabbing name and ext
223
- elements = file.split(".")
224
- if elements.size < 2
225
- name = elements.first
226
- ext = nil
227
- else
228
- name = elements[0...-1].join('.')
229
- ext = elements[-1, 1].first
230
- end
231
-
232
- # trimming the n last characters
233
- new = [name[0, name.length - n], ext].compact.join('.')
176
+ run(proc_basename, bn)
177
+ end
178
+
179
+ # Change file extensions
180
+ def ext(e)
181
+ proc_ext = Proc.new do |file, extension|
182
+ # We don't want to process dotfiles
183
+ unless File.basename(file) =~ /^\./
184
+ # We want to remove the last extension
185
+ name = file.split(".")
186
+ name = name[0...-1].join(".") if name.size > 1
187
+ name = name.to_s
188
+ # Adding the new extension
189
+ new = (extension == "" ? name : name + "." + extension)
234
190
  rename(file, new)
235
191
  end
236
- run(proc_trim_last, n)
237
192
  end
238
-
239
- # Add date (using time arg) to the beginning of the filename
240
- def current_date(time)
241
- proc_current_date = Proc.new do |file, date|
242
- new = [date, file].join('_')
243
- rename(file, new)
193
+ run(proc_ext, e)
194
+ end
195
+
196
+ # Trim first n characters
197
+ def trim_first(n)
198
+ proc_trim_first = Proc.new do |file, n|
199
+ # Grabbing name and ext
200
+ elements = file.split(".")
201
+ if elements.size < 2
202
+ name = elements.first
203
+ ext = nil
204
+ else
205
+ name = elements[0...-1].join('.')
206
+ ext = elements[-1, 1].first
244
207
  end
245
- run(proc_current_date, time.strftime("%Y-%m-%d"))
208
+
209
+ # trimming the n first characters
210
+ new = [name[n..-1], ext].compact.join('.')
211
+ rename(file, new)
246
212
  end
247
-
248
- # Add datetime (using time arg) to the beginning of the filename
249
- def current_datetime(datetime)
250
- proc_current_datetime = Proc.new do |file, datetime|
251
- new = [datetime, file].join('_')
252
- rename(file, new)
213
+ run(proc_trim_first, n)
214
+ end
215
+
216
+ # Trim last n characters
217
+ def trim_last(n)
218
+ proc_trim_last = Proc.new do |file, n|
219
+ # Grabbing name and ext
220
+ elements = file.split(".")
221
+ if elements.size < 2
222
+ name = elements.first
223
+ ext = nil
224
+ else
225
+ name = elements[0...-1].join('.')
226
+ ext = elements[-1, 1].first
253
227
  end
254
- run(proc_current_datetime, datetime.strftime("%Y-%m-%d_%H-%M"))
228
+
229
+ # trimming the n last characters
230
+ new = [name[0, name.length - n], ext].compact.join('.')
231
+ rename(file, new)
255
232
  end
233
+ run(proc_trim_last, n)
234
+ end
256
235
 
257
- private
258
- # Helper to test files and run the specific job
259
- def run(a_proc, *others)
260
- @filenames.each do |file|
261
- if File.exist?(file) then
262
- if File.writable?(file) then
263
- a_proc.call(file, *others)
264
- else
265
- warn _("%s: You don't have write access on %s") % [Const::NAME, file]
266
- end
267
- else
268
- warn _("%s: File %s doesn't exists!") % [Const::NAME, file]
269
- end
270
- end
236
+ # Add date (using time arg) to the beginning of the filename
237
+ def current_date(time)
238
+ proc_current_date = Proc.new do |file, date|
239
+ new = [date, file].join('_')
240
+ rename(file, new)
271
241
  end
242
+ run(proc_current_date, time.strftime("%Y-%m-%d"))
243
+ end
272
244
 
273
- # Helper to rename files
274
- def rename(old, new)
275
- unless File.exist?(new) and @overwrite == false
276
- # Expanding paths to make them reliable
277
- old = File.expand_path(old)
278
- new = File.join(File.dirname(old), File.basename(new))
279
- # Renaming the file
280
- File.rename(old, new)
281
- else
282
- warn EXISTS % [Const::NAME, new]
283
- end
245
+ # Add datetime (using time arg) to the beginning of the filename
246
+ def current_datetime(datetime)
247
+ proc_current_datetime = Proc.new do |file, datetime|
248
+ new = [datetime, file].join('_')
249
+ rename(file, new)
284
250
  end
251
+ run(proc_current_datetime, datetime.strftime("%Y-%m-%d_%H-%M"))
252
+ end
285
253
 
286
- # Helper to rename files ensuring compatibily on all FS
287
- def rename_fs(old, new)
288
- unless File.exist?(new) and @overwrite == false
289
- # Expanding paths to make them reliable
290
- old = File.expand_path(old)
291
- new = File.join(File.dirname(old), File.basename(new))
292
- # Renaming the file
293
- tf = Tempfile.new("renamer")
294
- File.rename(old, tf.path)
295
- File.rename(tf.path, new)
296
- tf.close(true)
254
+ private
255
+ # Helper to test files and run the specific job
256
+ def run(a_proc, *others)
257
+ @filenames.each do |file|
258
+ if File.exist?(file) then
259
+ if File.writable?(file) then
260
+ a_proc.call(file, *others)
297
261
  else
298
- warn EXISTS % [Const::NAME, new]
262
+ warn _("%s: You don't have write access on %s") % [Const::NAME, file]
299
263
  end
264
+ else
265
+ warn _("%s: File %s doesn't exists!") % [Const::NAME, file]
266
+ end
267
+ end
268
+ end
269
+
270
+ # Helper to rename files
271
+ def rename(old, new)
272
+ unless File.exist?(new) and @overwrite == false
273
+ # Expanding paths to make them reliable
274
+ old = File.expand_path(old)
275
+ new = File.join(File.dirname(old), File.basename(new))
276
+ # Renaming the file
277
+ File.rename(old, new)
278
+ else
279
+ warn EXISTS % [Const::NAME, new]
280
+ end
281
+ end
282
+
283
+ # Helper to rename files ensuring compatibily on all FS
284
+ def rename_fs(old, new)
285
+ unless File.exist?(new) and @overwrite == false
286
+ # Expanding paths to make them reliable
287
+ old = File.expand_path(old)
288
+ new = File.join(File.dirname(old), File.basename(new))
289
+ # Renaming the file
290
+ tf = Tempfile.new("renamer")
291
+ File.rename(old, tf.path)
292
+ File.rename(tf.path, new)
293
+ tf.close(true)
294
+ else
295
+ warn EXISTS % [Const::NAME, new]
300
296
  end
297
+ end
301
298
  end