How to remove a reoccuring word/character and what comes after, from the filenames of multiple files?

232 Views Asked by At

I have several folders of video files where, due to the download manager I use, they are all named in the following format "FILENAME.mp4; filename= FILENAME.mp4" All I've been trying to do is to remove everything after (and including) ".mp4; filename". However, I haven't found a way to do this.

I have tried some free software (such as Renamer, Namechanger, Name Munger for Mac, Transnomino) but I failed to do what I need to.

I'm working on Mac OSX 10.13.6.

Any help with this issue would be appreciated.

2

There are 2 best solutions below

6
Saik0s On BEST ANSWER

You can achieve it using Terminal. Go to the folder where you want to rename files using this cd command, for example:

cd ~/Documents/Videos

And run this command to rename all files recursively:

find . -iname "*.mp4;*" | sed -E 's/(\.[^\.]*)(\.mp4)(.*)/mv "\1\2\3" "\1\2"/' | sh

This command will keep only FILENAME.mp4 part from FILENAME.mp4; filename= FILENAME.mp4 file name

0
Caius Jard On

I used to extensively use a windows Rename tool called Renamer 6.0, and it had a "pattern rename" facility called "Multi change" that could have handled this.

In the context of that tool it would be asking for a source pattern like %a= %b and a destination pattern (like %b), everything after the = would be stored in %b variable and then renaming the file to just %b would lose everything after the =

See if your preferred rename tool has a similar facility?

If your tool supports regex, then find: .*?=(.*) and replace with $1

I'm also minded that asking this question on https://unix.stackexchange.com/ might elicit some help crafting a shell script that will perform this rename (though also plenty of shell capable people here, one of them may see it - it's just that it's not quite as hardcore programmer-y a question as most).

If you're willing to learn/use java, then that could be another good way to get the problem solved. It would (at a guess) look something like this:

    for (final File f : new File("C:\\temp").listFiles()) {
        if (f.isFile()) {
            string n = f.getName();
            if (n.contains("=")) {
                f.renameTo(new File(n.substring(n.indexOf("=")+1));
            }
        }
    }