1

I am trying to rename a lot of music files. The files contain spaces and capitals and whatnot and I want to replace the spaces with underscores and remove all capital letters. Is there a way to do that? I have seen changing extensions and multiple file names that are almost identical save for a few letters or numbers changed but nothing like what I need.

0
3

A small python script will do the renaming recursively:

#!/usr/bin/env python3
import os
import shutil
import sys

directory = sys.argv[1]

for root, dirs, files in os.walk(directory):
    for f in files:
        new_f = root+"/"+f.replace(" ", "_").lower()
        f = root+"/"+f
        if f != new_f:
            shutil.move(f, new_f)

where:

  • lower() will lower all possible capitals .
  • replace(" ", "_") will replace spaces by underscores.
  • if f != new_f will only rename the files if it is actually needed.
  • shutil.move(f, new_f) will actually rename the file.

How to use

  • Copy the script into an empty file
  • Save it as rename.py
  • Run it with the targeted directory as argument:

    python3 /path/to/rename.py <directory>
    

Notes

  • Use quotes if the targeted directory includes spaces.
  • Mind that you will run into name clashes if the renamed file name already exists.

In case of possible name clashes

(If you already started renaming manually for example) Use the version below.

#!/usr/bin/env python3
import os
import shutil
import sys

directory = sys.argv[1]

for root, dirs, files in os.walk(directory):
    for f in files:
        renamed = f.replace(" ", "_").lower() ;new_f = root+"/"+renamed
        old_f = root+"/"+f
        if old_f != new_f:
            n = 1
            while os.path.exists(new_f):
                new_f = root+"/dupe_"+str(n)+"_"+renamed
                n = n+1
            shutil.move(old_f, new_f)

It will renane:

enter image description here

into:

enter image description here




EDIT

The script above will rename files. From a comment, I understand you'd like to rename the folders likewise. With just a few lines, that can be done:

#!/usr/bin/env python3
import os
import shutil
import sys

directory = sys.argv[1]

def name_edit(item, root):
    renamed = item.replace(" ", "_").lower() ;new_item = root+"/"+renamed
    old_item = root+"/"+item
    if old_item != new_item:
        n = 1
        while os.path.exists(new_item):
            new_item = root+"/dupe_"+str(n)+"_"+renamed
            n = n+1
        shutil.move(old_item, new_item)

for root, dirs, files in os.walk(directory):
    for item in files:
        name_edit(item, root)
    for item in dirs:
        name_edit(item, root)

Explanation

In the second script, the way files were renamed is converted to a function, to prevent writing the same code twice (running it on both files/folders). Subsequently, the script first renames the files, then the folders recursively:

#!/usr/bin/env python3
import os
import shutil
import sys

directory = sys.argv[1]

def name_edit(item, root):
    # in one command, both replace spaces and lower {possible} capitals
    renamed = item.replace(" ", "_").lower()
    # combine directory and (new) file- or folder name
    new_item = root+"/"+renamed
    # combine directory and (old) file- or folder name
    old_item = root+"/"+item
    # if the name was changed, check for possible existing dupes
    # and rename until the name is unique
    if old_item != new_item:
        n = 1
        while os.path.exists(new_item):
            new_item = root+"/dupe_"+str(n)+"_"+renamed
            n = n+1
        # if the file or folder name was changed, apply the change
        shutil.move(old_item, new_item)

for root, dirs, files in os.walk(directory):
    # use os.walk() to find files and folders recursively
    for item in files:
        name_edit(item, root)
    for item in dirs:
        name_edit(item, root)
9
  • it doesn't seem to be working – Ron Deeds Nov 7 '15 at 10:10
  • actually it may have but it didn't do the directories like i was hoping – Ron Deeds Nov 7 '15 at 10:17
  • @RonDeeds ?? Could you explain? How doesn't it do the directories? The example in the image is a sub directory of the argument. [edit] tested it again, all sub and sub-sub directories are renamed correctly in my tests. Please let me know if- and what goes wrong. – Jacob Vlijm Nov 7 '15 at 10:30
  • It changed all of the files in the main directory but not the subdirectories the files were in. I.e. it changed the mp3 files in '3 Doors Down' subdirectory in the 'Downloads' directory, but not the '3 Doors Down' subdirectory itself. I don't know if that makes sense or not. But I was hoping it would change the name of the subdirectory as well as the files within the subdirectory if that helps my explanation at all. – Ron Deeds Nov 7 '15 at 21:50
  • You mentioned Can you change multiple dissimilar file names?, I deliberately left alone the folder (directory-) names. Will edit the answer probably tomorrow. – Jacob Vlijm Nov 7 '15 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.