Created
June 18, 2024 13:22
-
-
Save DiogenesAnalytics/dd9d5345e87d26cc4d6bb230fe892e36 to your computer and use it in GitHub Desktop.
Mass rename Nest Camera clips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from datetime import datetime | |
# Directory containing the files | |
directory = "." | |
# Compile a regex to match the date string within the filename | |
filename_regex = re.compile(r"Clip \((June \d+ \d+ at \d+ [APM]+)\)\.mp4") | |
# Function to convert the date/time format | |
def convert_date_format(date_str): | |
# Parse the input date string | |
date_obj = datetime.strptime(date_str, "%B %d %Y at %I%M %p") | |
# Format the date object to the desired output format | |
return date_obj.strftime("%Y%m%d%H%M") | |
# Function to replace whitespace and remove parentheses from the original filename part | |
def clean_original_name(text): | |
return text.replace('(', '').replace(')', '').replace(' ', '_') | |
# Iterate over the files in the directory | |
for filename in os.listdir(directory): | |
# Match the filenames using regex | |
match_obj = filename_regex.search(filename) | |
if match_obj: | |
# Extract the date string | |
date_str = match_obj.group(1) | |
# Convert the date format | |
new_date_str = convert_date_format(date_str) | |
# Clean the original filename part | |
original_filename_part = clean_original_name(filename[:match_obj.end()]) | |
# Generate the new filename | |
new_filename = f"{original_filename_part}_{new_date_str}.mp4" | |
# Create full paths | |
old_file_path = os.path.join(directory, filename) | |
new_file_path = os.path.join(directory, new_filename) | |
# Rename the file | |
os.rename(old_file_path, new_file_path) | |
print(f"Renamed: {old_file_path} -> {new_file_path}") | |
print("Renaming completed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment