Skip to main content

Regular expressions bulk rename tool for multiple files

Project description

regex-rename

Bulk rename tool based on regular expressions to rename multiple files at once.

GitHub version PyPI version Build Status codecov

Quickstart

Renaming multiple files at once:

$ ls # awful names:
b45XDS-01.mp3  QsEW2s-02.mp3  VF7t6L-03.mp3

$ regex-rename '-(\d+).mp3' '\1_NeverGonnaGiveYouUp.mp3' --rename                                                                                venv
[2022-04-09 09:19:15] DEBUG matching regex pattern pattern=-(\d+).mp3 replacement=\1_NeverGonnaGiveYouUp.mp3 full_match=False padding=None testing_mode=False
[2022-04-09 09:19:15] INFO  renaming file from=QsEW2s-02.mp3 to=02_NeverGonnaGiveYouUp.mp3
[2022-04-09 09:19:15] INFO  renaming file from=VF7t6L-03.mp3 to=03_NeverGonnaGiveYouUp.mp3
[2022-04-09 09:19:15] INFO  renaming file from=b45XDS-01.mp3 to=01_NeverGonnaGiveYouUp.mp3
[2022-04-09 09:19:15] INFO  files renamed count=3

$ ls # now we're talking:
01_NeverGonnaGiveYouUp.mp3  02_NeverGonnaGiveYouUp.mp3  03_NeverGonnaGiveYouUp.mp3

Installation

pip3 install regex-rename

It requires Python 3.7 (or newer) with pip.

Example

Imagine you've got audio files awfully named like this:

  • Stanis▯aw+Lem+Invincible+(1).mp3
  • Stanis▯aw+Lem+Invincible+(2 ).mp3
  • Stanisław_Lem_Invincible (3) .mp3
  • Stanis▯aw+Lem+Invincible+(51).mp3

and you want to rename all of them in a manner: 01 The Invincible.mp3, 02 The Invincible.mp3, … (specifically to extract the number, put it at the beginning, and apply 2-digits padding to it).

Step 1: Check the matching pattern

The Regex pattern to match these files and extract episode number from parentheses may be as follows: (\d+).*mp3 (contains number, ends with mp3)

Let's check if the files are matched properly: regex-rename '(\d+).*mp3'
Usage example

Pay attention to the extracted regex groups.

Step 2: Check the replacement pattern

We'd like to replace all files to a pattern: \1 The Invincible.mp3 (\1 is a first extracted group from matching pattern).

Regex can't easily pad numbers with zeros. Fortunately, we can use --pad-to=2 parameter to obtain 2-digit numbers.

Let's test it: regex-rename '(\d+).*mp3' '\1 The Invincible.mp3' --pad-to=2
Usage example

Step 3: Actual renaming

All above commands were just testing our patterns so that we could experiment with regex patterns. Once we're sure that everything is matched correctly, we can use --rename flag, which does the actual renaming:
regex-rename '(\d+).*mp3' '\1 The Invincible.mp3' --pad-to=2 --rename
Usage example

Finally, files are named properly:

  • 01 The Invincible.mp3
  • 02 The Invincible.mp3
  • 03 The Invincible.mp3
  • 51 The Invincible.mp3

Beyond the Regex

regex-rename also supports some transformations not covered by regular expressions standard:

  • Converting to lowercase by adding \L before group number:
    regex-rename '([A-Z]+).mp3' '\L\1.mp3'
    eg. AUDIO.mp3 to audio.mp3
  • Converting to uppercase by adding \U before group number:
    regex-rename '([a-z]+).mp3' '\U\1.mp3'
    eg. audio.mp3 to AUDIO.mp3
  • Padding numbers with leading zeros by adding \P2, \P3, … (depending on padding length) before group number:
    regex-rename '(\d+).mp3' '\P2\1.mp3'
    eg. 1.mp3 to 01.mp3
  • Padding numbers with leading zeros by specifying --pad-to parameter:
    regex-rename '(\d+).mp3' '\1.mp3' --pad-to=2
    eg. 1.mp3 to 01.mp3

More examples

  • Extract episode number:
    regex-rename '(\d+)' '\1.mp3' --rename
    eg. episode12HQ.mp3 to 12.mp3
  • Swap artist with title:
    regex-rename '([^-]+) - ([^-]+)\.mp3' '\2 - \1.mp3' --rename
    eg. Echoes - Pink Floyd.mp3 to Pink Floyd - Echoes.mp3
  • Pad leading zeros:
    regex-rename '(\d+).mp3' '\P3\1.mp3' --rename
    eg. 1.mp3 to 001.mp3
  • Convert to lowercase:
    regex-rename '(.+)' '\L\1' --rename
    eg. SONG.MP3 to song.mp3
  • Convert to uppercase:
    regex-rename '(.+)\.mp3' '\U\1.mp3' --rename
    eg. Tool.mp3 to TOOL.mp3
  • Add prefix:
    regex-rename '(.+)' 'The \1' --full --rename
    eg. Doors.mp3 to The Doors.mp3
  • Change extension:
    regex-rename '(.+)\.apk' '\1.zip' --rename
    eg. Songbook.apk to Songbook.zip
  • Turn directories into prefixes and move files:
    regex-rename '(.+)/(.+).mp3' '\1 - \2.mp3' --full --recursive --rename
    eg. Pink Floyd/Echoes.mp3 to Pink Floyd - Echoes.mp3

Usage

enter regex-rename for help:

$ regex-rename 
regex-rename v1.0.0 (nuclear v1.2.3) - Bulk rename tool based on regular expressions to rename multiple files at once

Usage:
regex-rename [OPTIONS] PATTERN [REPLACEMENT]

Arguments:
   PATTERN       - Regex pattern to match filenames
   [REPLACEMENT] - Replacement regex pattern for renamed files. Use \1, \2 syntax to make use of matched groups

Options:
  --version                   - Print version information and exit
  -h, --help [SUBCOMMANDS...] - Display this help and exit
  -r, --rename                - Does actual renaming files instead of just testing replacement pattern
  --full                      - Enforces matching full filename against pattern
  --recursive                 - Search directories recursively
  --pad-to PAD_TO             - Applies padding with zeros with given length on matched numerical groups

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

regex-rename-1.0.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

regex_rename-1.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file regex-rename-1.0.0.tar.gz.

File metadata

  • Download URL: regex-rename-1.0.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for regex-rename-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aad7290a00ff476dac53e1256d76432e2180e69e49c00b35f96756b789c43e78
MD5 fb0aed880f2ac16fc6cd432f21163c42
BLAKE2b-256 098430461fe1a89a491f3e6dd36080154b3932ad1748da8397166b859d74c382

See more details on using hashes here.

File details

Details for the file regex_rename-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: regex_rename-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for regex_rename-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 baaecf235bb344186c570cfc9136766fbd6c96ce42dd2d1eb408dedee5e7a668
MD5 90b615985df2b9374287d51699005c60
BLAKE2b-256 96a165f3d8645dd61e99a9804d2b7ca8ef3297074b06b6f81aeb22b853a09887

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page