-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathFile_Renamer.py
31 lines (25 loc) · 1.24 KB
/
File_Renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import re
# Prompt the user for the directory path where files need to be renamed
directory = input("Enter path: ")
# Provide instructions on how to use regex for searching specific files
print("To look for specific files, enter what you know, using .* for any characters you don't know.")
print("For example: IMG.* will filter files that start with IMG")
# Get the regex pattern to match files and the new base name for renaming
pattern = input("Enter pattern: ")
new_name = input("Enter the new name: ")
def rename_files(directory, pattern, new_name):
# List all files in the specified directory
files = os.listdir(directory)
counter = 0 # Initialize a counter for unique naming
# Iterate over each file in the directory
for file in files:
# Check if the file matches the given pattern
if re.match(pattern, file):
# Get the file extension
filetype = file.split('.')[-1]
# Rename the file with the new base name and counter
os.rename(directory + '/' + file, directory + '/' + new_name + str(counter) + '.' + filetype)
counter += 1 # Increment the counter for the next file
# Call the function to rename files
rename_files(directory, pattern, new_name)