Module: Mvb
- Defined in:
- lib/mvb.rb,
lib/mvb/version.rb
Constant Summary collapse
- COLOR_RED =
"\e[31m"
- COLOR_GREEN =
"\e[32m"
- COLOR_RESET =
"\e[0m"
- PH =
for “PlaceHolder”
"#"
- VERSION =
"0.0.3"
Class Method Summary collapse
-
.find_matching_files(pattern) ⇒ Object
Find array of matching filenames based on regex parttern.
-
.print_filenames(filenames) ⇒ Object
Print array of filenames.
-
.rename_files(search_pattern, replacement = nil, force_rename = false, steps = 1) ⇒ Object
Rename matched files according to search (regex) parttern, replacement, and force flag if force flag is not given, provide just a list (i.e dry-run).
Class Method Details
.find_matching_files(pattern) ⇒ Object
Find array of matching filenames based on regex parttern
11 12 13 14 |
# File 'lib/mvb.rb', line 11 def self.find_matching_files(pattern) return [] if pattern.nil? Dir.glob('*').select { |filename| filename.match?(pattern) } end |
.print_filenames(filenames) ⇒ Object
Print array of filenames
17 18 19 20 21 22 23 |
# File 'lib/mvb.rb', line 17 def self.print_filenames(filenames) if filenames.empty? puts "No match found." return end filenames.each { |filename| puts "#{filename}"} end |
.rename_files(search_pattern, replacement = nil, force_rename = false, steps = 1) ⇒ Object
Rename matched files according to search (regex) parttern, replacement, and force flag if force flag is not given, provide just a list (i.e dry-run)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mvb.rb', line 27 def self.rename_files(search_pattern, replacement=nil, force_rename=false, steps=1) # No search pattern given? Return nothing if search_pattern == nil puts "You have to provide at least one regex pattern.\nPlease check 'mvb --help' for command usage." return end # Get array with matched files based on regex pattern matched_files = find_matching_files(search_pattern) # No match found on search pattern? Return nothing if matched_files.empty? puts "No match found." return end # No replacement string given? Print only the matched filenames if replacement == nil print_filenames(matched_files) return end # If replacement include the "#" placeholder, define the starting point (index) if replacement.include?(PH) placeholder_present = true new_filenames = [] # The index will be equal to the number that is after the placeholder, e.g. "#2", index = 2 index = replacement[/(?<=#{PH})\d+/] index = index.to_i steps = 1 if steps == 0 or steps == nil # Just in case end # Here is where the renaming (or dry-run) happens... puts "\n" # insert blank line between the command and the list of filenames matched_files.each do |filename| # Remember: "filename.gsub(...)" returns a copy of the filename with all the occurrences of a # given regex pattern substituted for the second argument new_filename = filename.gsub(search_pattern,replacement) # If placeholder is present in new_filename, execute sequential numbering if new_filename.include?(PH) if new_filename[/#{PH}\d+/] != nil new_filename.gsub!(/#{PH}\d+/,index.to_s) else new_filename.gsub!(PH,index.to_s) end index = index + steps end # List every filename along with their possible replacement puts "#{COLOR_RED}#{filename}#{COLOR_RESET} --> #{COLOR_GREEN}#{new_filename}#{COLOR_RESET}" # If force flag is given, execute rename if force_rename if placeholder_present new_filename = new_filename + "_" File.rename(filename,new_filename) new_filenames.push(new_filename) else File.rename(filename,new_filename) end end end if placeholder_present and force_rename new_filenames.each do |filename| File.rename(filename,filename.chomp("_")) end end # Provide message if files were renamed or if it was a dry-run if force_rename puts "\nFiles renamed!" else puts "\nThis is a dry-run. No file has been renamed.\nAdd -f to execute rename." end end |