Home » Blog
date 17.Jul.2022

■ Remove the beginning part of a filename, in bulk


You can rename files one by one, but sometimes filenames contain certain patterns you can exploit to rename them all in one go, saving time and mental energy. xplorer² mass rename wizard lets you modify filenames with simple transformations such as: In today's article we're going to focus on eliminating parts of the filename that are unnecessary. We're going to search for something and replace it with nothing, effectively wiping out the unwanted part. For ease of exhibition we'll concentrate on removing the leading bit (start) of filenames — but the same ideas can be used for middle or trailing parts.

1. Easy case: fixed name

Let's say we have MP3 files that begin with the artist's name such as

Freddie Hubbard - Far Away.mp3
Freddie Hubbard - D Minor Mint.mp3
...
As we have the artist's name in the folder containing the tracks, we don't need it in each individual song. And as all files start with the same text Freddie Hubbard - (notice we include the separator dash — for removal), we select all MP3s and press <F2> key (or File > Mass rename menu) and set up a search and replace transformation as in the picture:

replace fixed filename part

Note we set the mass rename dialog in Match and replace part mode, we entered the part to find in What to match box and we left the Target name template box empty, so that we eliminate the string. Hit Preview button to see what this setup does: it converts Freddie Hubbard - Far Away.mp3 into Far Away.mp3. If you are happy, click on Rename button to go ahead and change the songs.

2. Medium case: variable name with fixed length

The following rename task calls again for eliminating the artist name, but see that we also have a variable number to remove (101, 102 etc)

101-eberhard_weber-touch.mp3
102-eberhard_weber-sand_glass.mp3
103-eberhard_weber-yellow_fields.mp3
...
We can't just search for a fixed text this time. However we can exploit the fact that the variable part to be removed has a fixed length (19 characters long from the beginning). So we search for the special character range token that corresponds to these letters, which is $[1:19] and again replace it with nothing:

replace set filename length

The format of the character range token includes the starting letter (position 1), followed by a colon and then the number of characters to match (19), which explains why we used 1:19 to match the first 19 letters.

The range token supports matching the end of the string too, using negative numbers, e.g. $[-5:5] would match the last 5 characters of the filename

3. Advanced case: variable leading bit

In many cases there's no easy pattern to exploit with simple search and replace. There are many possibilities but let us consider a variable leading portion that is delimited by a separator character, e.g. a dash (-). Notice how the text to be removed doesn't have a fixed length either:

123-file1.txt
abcde-file2.txt
3210-file3.txt
...
To deal with this case we use a regular expression that matches everything from the beginning and up to the separator dash, which is:
^.*\-

Regular expressions aren't my forte, but they are a bit like DOS wildcards with rich syntax. In the above regexp the following special characters are used:

^   matches the beginning of the filename
.*   matches any character, any number of times (wildcard)
\-   matches the dash; since - is a special regexp symbol we need to escape it with a backslash

If you add all the above together, our regexp matches both the number 123- and the letters abcde-, and in general everything that comes before the dash! The mass rename dialog setup is similar, only notice we've ticked RE (regexp) box for the special search string:

replace variable filename part with regular expression

If you want to operate on files from multiple folders all at once, put all the files in a scrap container, e.g. using File > Browse flat menu) and select all of them. Just make sure all filenames have the same format for the search and replace pattern you use

Post a comment on this topic »

Share |

©2002-2022 ZABKAT LTD, all rights reserved | Privacy policy | Sitemap