Skip to main content

Regular expressions bulk rename tool for multiple files

Project description

regex-rename

Regular expressions bulk rename tool for multiple files

GitHub version PyPI version Build Status codecov

Quickstart

Renaming multiple files at once:

$ ls
01.mp3  02.mp3

$ regex-rename --rename '(\d+).mp3' '\1_Greatest_Hits.mp3'
[2021-03-04 00:27:23] [DEBUG] matching regex pattern testing_mode=False pattern=(\d+).mp3 replacement=\1_Greatest_Hits.mp3 full_match=False padding=None
[2021-03-04 00:27:23] [INFO ] renaming file from=01.mp3 to=01_Greatest_Hits.mp3
[2021-03-04 00:27:23] [INFO ] renaming file from=02.mp3 to=02_Greatest_Hits.mp3
[2021-03-04 00:27:23] [INFO ] files renamed count=2

$ ls
01_Greatest_Hits.mp3  02_Greatest_Hits.mp3

Usage

enter regex-rename for help:

$ regex-rename 
regex-rename v0.1.1 (nuclear v1.1.5) - Regular expressions bulk rename tool for multiple files

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
  --pad-to PAD_TO             - Applies padding with zeros with given length on matched numerical groups

Installation

pip3 install regex-rename

Requirements:

  • Python 3.6 (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 (extracting number from the end and put it at the beginning and padding it to 2 digits by the way).

Step 1: Testing matching pattern

Our Regex pattern to match those files and extract number from parentheses should be like this: .+\((\d+) ?\).+

Let's test matching pattern: regex-rename '.+\((\d+) ?\).+'
Usage example
Notice that regex groups are extracted in logs.

Step 2: Testing 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 to obtain 2-digit numbers

Let's test it: regex-rename '.+\((\d+) ?\).+' '\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. Only when we're sure that everything is matched correctly, we can use --rename flag which does the actual renaming:
regex-rename '.+\((\d+) ?\).+' '\1 The Invincible.mp3' --pad-to=2 --rename
Usage example

From now 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'
  • Converting to uppercase by adding \U before group number:
    regex-rename '([a-z]+).mp3' '\U\1.mp3'
  • Padding numbers with zeros by specifying --pad-to parameter:
    regex-rename '(\d+).mp3' '\1.mp3' --pad-to=2

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-0.1.2.tar.gz (4.6 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-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: regex-rename-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.3

File hashes

Hashes for regex-rename-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d23ee46333394c2c2a1315611cbcf85a448dd1ae45b8bbdd608825789f3547c0
MD5 d5977f1e7b5fe9f0501140c1f1d163b7
BLAKE2b-256 f23ffbfb57bf6c6a162f3a433b9a2067e81e0d42a09ed7e89718c163193efa66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: regex_rename-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.3

File hashes

Hashes for regex_rename-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e37a4c26d2977ab8a328a2927e4ca112e5f5bdd06a0047fa5a3e24ce7f9831fd
MD5 55082ed1565ee9156c39cbf7174085bc
BLAKE2b-256 40c1ef9e58d583915df1691e61855a67ae23438c54dc54070aec1c2691f8f59f

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