diff --git a/duplicate_files_in_folders/duplicates_finder.py b/duplicate_files_in_folders/duplicates_finder.py index 7cfc3a1..546c742 100644 --- a/duplicate_files_in_folders/duplicates_finder.py +++ b/duplicate_files_in_folders/duplicates_finder.py @@ -177,16 +177,16 @@ def process_duplicates(combined: Dict, args: Namespace) -> (int, int): # Copy or move files to reference locations if not args.copy_to_all: - copy_or_move_file(ref_files[0]['path'], args.move_to, src_filepath, args.reference_dir, move=True) + copy_or_move_file(src_filepath, args.move_to, ref_files[0]['path'], args.reference_dir, move=True) files_moved += 1 else: num_to_copy = max(0, len(ref_files) - len(srcs_to_move)) for i in range(num_to_copy): - copy_or_move_file(ref_files[i]['path'], args.move_to, src_filepath, args.reference_dir, False) + copy_or_move_file(src_filepath, args.move_to, ref_files[i]['path'], args.reference_dir, False) files_created += 1 for (src, _), tgt in zip(srcs_to_move, ref_files[num_to_copy:]): - copy_or_move_file(tgt['path'], args.move_to, src, args.reference_dir, move=True) + copy_or_move_file(src, args.move_to, tgt['path'], args.reference_dir, move=True) files_moved += 1 return files_moved, files_created diff --git a/duplicate_files_in_folders/old_duplicates_finder.py b/duplicate_files_in_folders/old_duplicates_finder.py index 4f84c98..aca2f5c 100644 --- a/duplicate_files_in_folders/old_duplicates_finder.py +++ b/duplicate_files_in_folders/old_duplicates_finder.py @@ -125,18 +125,18 @@ def move_to_ref_paths(args, src_filepath, ref_paths_to_copy, scan_duplicates, fi scan_duplicates.sort(key=lambda x: x[0], reverse=True) # sort by path name reverse for easier testing if not args.copy_to_all: - copy_or_move_file(ref_paths_to_copy[0], args.move_to, src_filepath, args.reference_dir) + copy_or_move_file(src_filepath, args.move_to, ref_paths_to_copy[0], args.reference_dir) return files_created, files_moved + 1 num_to_copy = max(0, len(ref_paths_to_copy) - len(scan_duplicates)) if num_to_copy: # Copy first scan_dir to make up for fewer scan_dir duplicates for i in range(num_to_copy): - copy_or_move_file(ref_paths_to_copy[i], args.move_to, src_filepath, args.reference_dir, False) + copy_or_move_file(src_filepath, args.move_to, ref_paths_to_copy[i], args.reference_dir, False) files_created += 1 # Move each scan_dir duplicate to the corresponding ref path for (src, _), tgt in zip(scan_duplicates, ref_paths_to_copy[num_to_copy:]): - copy_or_move_file(tgt, args.move_to, src, args.reference_dir, move=True) + copy_or_move_file(src, args.move_to, tgt, args.reference_dir, move=True) files_moved += 1 return files_created, files_moved diff --git a/duplicate_files_in_folders/utils.py b/duplicate_files_in_folders/utils.py index 4918159..c86c27c 100644 --- a/duplicate_files_in_folders/utils.py +++ b/duplicate_files_in_folders/utils.py @@ -309,14 +309,16 @@ def setup_file_manager(args: Namespace): return fm -def copy_or_move_file(ref_file_path: str, destination_base_path: str, scan_file_path: str, base_ref_path: str, +def copy_or_move_file(scan_file_path: str, destination_base_path: str, ref_file_path: str, base_ref_path: str, move: bool = True) -> str: """ Copy or move a file from the source to the reference directory. - :param ref_file_path: - :param destination_base_path: - :param scan_file_path: - :param base_ref_path: + :param scan_file_path: Full path of the file we want to copy/move + :param ref_file_path: The full path to the reference file within the base reference directory. + This path is used to determine the relative path for the destination. + :param destination_base_path: The base path where the file should be copied or moved to. + :param base_ref_path: The base directory path of the reference files. + This is used to calculate the relative path of the ref_file_path. :param move: True to move the file, False to copy it :return: the final destination path """ @@ -333,17 +335,16 @@ def copy_or_move_file(ref_file_path: str, destination_base_path: str, scan_file_ return final_destination_path -def check_and_update_filename(original_filename: str) -> str: +def check_and_update_filename(original_filename: str, renaming_function=lambda original_filename: f"{os.path.splitext(original_filename)[0]}_{int(time.time())}{os.path.splitext(original_filename)[1]}") -> str: """ Check if the filename already exists and rename it to avoid overwriting. - :param original_filename: + :param original_filename: the original filename + :param renaming_function: function that receives the original filename and returns the new filename :return: """ new_filename = original_filename if os.path.exists(original_filename): - timestamp = int(time.time()) # Get current Unix timestamp - base, ext = os.path.splitext(original_filename) - new_filename = f"{base}_{timestamp}{ext}" # Append timestamp to the filename + new_filename = renaming_function(original_filename) logger.info(f"Renaming of {original_filename} to {new_filename} is needed to avoid overwrite.") return new_filename