Batch renaming is an effective method for organizing your digital files. It can be especially useful when your files lack descriptive or consistent filenames.
You can automate a batch renaming action with a simple Python script. Pass a pattern into the Python script, and allow it to rename all files in a folder using a consistent naming convention.
Once the Python script is ready, you can run it on the command line, and it will rename all files in a specified folder.
How to Loop Through All Files in a Directory
There are so many ways that you can batch rename a file in Windows, including through the command prompt, or the Windows file explorer.
Another way you can rename files is by using a Python script. If you are unfamiliar with Python, there are ways you can learn how to write better Python code.
To rename all files in a given folder, you will need to loop through that set of files. You can view the full example in this GitHub repo.
-
Create a new folder to store the files that you want to rename:
- Create a new file called batch-rename.py.
-
At the top of the file, import the os module. This will allow you to access your operating system's files and directories:
import os
-
Define the directory where you have stored the files:
dir_path = "C:\\Users\\Sharl\\Desktop\\files"
You can also use a relative path instead. For example, if your script and files folder is under the same directory, your file path may look something like this:dir_path = "files"
-
Initialize a counter variable, which you will use to append a count at the end of the filename:
counter = 1
-
Add a for-loop to iterate over each file in the directory:
for filename in os.listdir(dir_path):
print("Renaming: " + filename + "...") -
To test the script, run it on the command line using the python command. Make sure you navigate to the folder location where you stored your script:
cd Desktop
python batch-rename.py
How to Rename All Files Based on a Given Pattern
The user will need to enter a pattern into the script, such as "Financial_Planning". The script will rename all the files to the provided pattern, and append a count at the end of the filename.
-
At the top of the file, import the sys module.
import sys
python batch-rename.py "Financial_Planning"
-
After the import statements, get the command-line argument entered. If the user does not enter one, print out an error message:
commandLineArgs = sys.argv
if len(commandLineArgs) > 1:
pattern = commandLineArgs[1] + "_{}"
else:
print('Enter a pattern for the new filenames as a command line argument')
sys.exit() -
Inside the for-loop, when looping through each file, get its file extension:
file_ext = os.path.splitext(filename)[1]
-
Create a new filename based on the given pattern. Add a count at the end of the filename, and re-add the file extension:
new_filename = pattern.format(counter) + file_ext
-
Rename the file with the new filename:
oldFileName = os.path.join(dir_path, filename)
newFileName = os.path.join(dir_path, new_filename)
os.rename(oldFileName, newFileName) -
Increment the counter for the next file:
counter += 1
-
After the for-loop, print out a confirmation message:
print("All files renamed.")
-
On a command line, navigate to the folder where you have stored your Python script. Use the python command, followed by a pattern to rename your files:
-
Open the file explorer and view the renamed files:
Automating Simple Tasks Using Python
A Python script is a very useful way to automate simple tasks, such as renaming multiple files at the same time. You can explore other ways you can batch rename files on your computer.