Module: RenameFilesGem

Defined in:
lib/rename_files_gem.rb,
lib/rename_files_gem/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.rename_files_with_pattern(folder_path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rename_files_gem.rb', line 11

def self.rename_files_with_pattern(folder_path)
  unless Dir.exist?(folder_path)
    puts "The specified path does not exist or is not a directory."
    return
  end

  Dir.glob("#{folder_path}/**/*").each do |file_path|
    next unless File.file?(file_path) && file_path.include?("(")

    file_name = File.basename(file_path, ".*")
    file_extension = File.extname(file_path)

    if match = file_name.match(/(.+)\((\d+)\)/)
      new_file_name = "#{match[1].strip}-#{match[2].to_i + 1}#{file_extension}"
      new_file_path = File.join(File.dirname(file_path), new_file_name)

      FileUtils.mv(file_path, new_file_path)
      puts "Renamed #{File.basename(file_path)} to #{new_file_name}"
    end
  end
end