Last active
January 10, 2026 09:24
-
-
Save Nereare/3968498ebbd96193c5b21d0a91b6ef4e to your computer and use it in GitHub Desktop.
Ruby Batch Rename Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # frozen_string_literal: true | |
| # Rename batch-downloaded music files (obviously acquired in a lawful manner as a righteous | |
| # citizen who believes strongly in the Free Market, the Invisible Hand, and the Capitalism | |
| # Gods). | |
| # | |
| # This class assumes certain points: | |
| # | |
| # 1. All the files pertain to the same artist or collection — _i.e._ OST; | |
| # 2. All the files may contain an ID suffix in the format `[alphanumeric]`; and | |
| # 3. All the files may contain invalid characters, such as `“` or `’`. | |
| class BatchRenameFiles | |
| # Filenames' extension. | |
| FILE_EXT = 'mp3' | |
| # Artist name to prefix to the song name — assumption 1. | |
| PREFIX = 'Artist Name' | |
| # Invalid characters (either String or Regexp) to replace by specific ones — assumption 3. | |
| INVALID_CHARS = [ | |
| [/\s{2,}/, ' '], | |
| [/[“”]/, "\""], | |
| [/[‘’]/, "\'"], | |
| [/`/, '-'], | |
| [/[–—]/, '-'] | |
| ].freeze | |
| # Instantiates class. | |
| def initialize | |
| @files = process_files | |
| end | |
| private | |
| # Gets all files to rename. | |
| def process_files | |
| list = Dir["*.#{FILE_EXT}"] | |
| raise Exception.new('No files retrieved!') unless list. | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment