Compare commits
2 Commits
31d60955a7
...
9a3775bf5d
Author | SHA1 | Date | |
---|---|---|---|
9a3775bf5d | |||
ffc8664553 |
@ -1,20 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Old version:
|
||||
# for f in ./*'('*')'*; do mv -i "$f" "${f/ (*)/}"; done
|
||||
# for f in ./*/*'('*')'*; do mv -i "$f" "${f/ (*)/}"; done
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
# Recursively find all files with parentheses in the name
|
||||
find . -type f -name '*(*' -print0 | while IFS= read -r -d '' file; do
|
||||
# Extract the new filename without parentheses
|
||||
new_name=$(echo "$file" | sed 's/ ([^)]*)//g')
|
||||
|
||||
# Check if the new name is different from the current name
|
||||
if [ "$file" != "$new_name" ]; then
|
||||
# Rename the file
|
||||
mv -i "$file" "$new_name"
|
||||
echo "Renamed: $file to $new_name"
|
||||
fi
|
||||
done
|
@ -1,33 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Recursively find all files with parentheses in the name
|
||||
find . -type f -name '*(*' -print0 | while IFS= read -r -d '' file; do
|
||||
# Extract the date from the parentheses using awk
|
||||
date_in_parentheses=$(echo "$file" | awk -F'[()]' '{print $2}')
|
||||
|
||||
# Convert the date to a format that can be compared
|
||||
formatted_date=$(date -d "$date_in_parentheses" +"%Y_%m_%d %H_%M_%S %Z" 2>/dev/null || echo "$date_in_parentheses")
|
||||
|
||||
# Check if the formatted date is valid
|
||||
if [ -n "$formatted_date" ]; then
|
||||
# Create a temporary directory for duplicates
|
||||
temp_dir=$(mktemp -d)
|
||||
|
||||
# Move all files with the same content inside parentheses to the temporary directory
|
||||
find . -type f -name "*($date_in_parentheses)" -exec mv -i {} "$temp_dir" \;
|
||||
|
||||
# Find the newest file in the temporary directory
|
||||
newest_file=$(ls -t "$temp_dir" | head -n 1)
|
||||
|
||||
# Move the newest file back to the original location
|
||||
if [ -n "$newest_file" ]; then
|
||||
mv -i "$temp_dir/$newest_file" "$file"
|
||||
echo "Kept: $file and removed older duplicates"
|
||||
fi
|
||||
|
||||
# Remove the temporary directory
|
||||
rm -r "$temp_dir"
|
||||
else
|
||||
echo "Skipping $file due to invalid date format in parentheses"
|
||||
fi
|
||||
done
|
67
win_fh_rename.py
Executable file
67
win_fh_rename.py
Executable file
@ -0,0 +1,67 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
# Solution inspired by: https://gist.github.com/kmorcinek/2710267
|
||||
|
||||
# This script will convert the date between the parentehsis and get the oldest date
|
||||
# It will delete the older files and search all sub directories
|
||||
# This definitely isn't the most efficient, but can process tens of thousands of files in less than a second
|
||||
|
||||
import re
|
||||
from os.path import join
|
||||
import os.path
|
||||
from os import walk, rename, remove
|
||||
import os
|
||||
import sys
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
FOLDER_PATH = os.getcwd()
|
||||
|
||||
processed = []
|
||||
|
||||
for path, subdirs, files in walk(FOLDER_PATH):
|
||||
for i, name in enumerate(files): # Iterate over the original list
|
||||
|
||||
if name in processed:
|
||||
continue
|
||||
|
||||
print('{} / {}'.format(i + 1, len(files)))
|
||||
|
||||
# Extract the date from the parentheses using regex
|
||||
match = re.search(r'\((\d{4}_\d{2}_\d{2} \d{2}_\d{2}_\d{2} UTC)\)', name)
|
||||
if match:
|
||||
date_in_parentheses = match.group(1)
|
||||
|
||||
# Convert the date to a datetime object for comparison
|
||||
formatted_date = datetime.strptime(date_in_parentheses, "%Y_%m_%d %H_%M_%S %Z")
|
||||
|
||||
# Find all files that match regex
|
||||
duplicates = [f for f in files if re.sub(r' \(.+\)', '', f) == re.sub(r' \(.+\)', '', name)]
|
||||
|
||||
# Find the newest file among duplicates
|
||||
newest_file = max(duplicates, key=lambda f: datetime.strptime(re.search(r'\((\d{4}_\d{2}_\d{2} \d{2}_\d{2}_\d{2} UTC)\)', f).group(1), "%Y_%m_%d %H_%M_%S %Z"))
|
||||
print("Newest file found:", newest_file)
|
||||
|
||||
# Rename the current file if needed
|
||||
new_name = re.sub(r' \(.+\)', '', newest_file)
|
||||
|
||||
# Move older duplicates to the fixed directory
|
||||
for duplicate in duplicates:
|
||||
duplicate_path = join(path, duplicate)
|
||||
if duplicate != newest_file:
|
||||
# Check if file exists because it can be deleted already
|
||||
if os.path.isfile(duplicate_path):
|
||||
remove(duplicate_path)
|
||||
print(f"Removed older duplicate: {duplicate_path}")
|
||||
else:
|
||||
if os.path.isfile(duplicate_path):
|
||||
rename(join(path, newest_file), join(path, new_name))
|
||||
print(f"Renamed: {duplicate_path} to {join(path, new_name)}")
|
||||
|
||||
processed.append(duplicate_path)
|
||||
|
||||
|
||||
else:
|
||||
print(f"Skipping {join(path, name)}")
|
||||
|
||||
print(f"Processed {len(processed)} files.")
|
Reference in New Issue
Block a user