From cb3edcaacf012700f8b68e45c7bc9599279e9798 Mon Sep 17 00:00:00 2001 From: akr1999 Date: Thu, 21 Mar 2024 20:38:56 +0530 Subject: [PATCH] Updated move function overwrite bug --- JDCN_FileMover.py | 41 ++++++++++++++++++++++++----------------- JDCN_VHSFileMover.py | 42 +++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/JDCN_FileMover.py b/JDCN_FileMover.py index c5de0ba..40f0974 100644 --- a/JDCN_FileMover.py +++ b/JDCN_FileMover.py @@ -13,28 +13,35 @@ def create_folder_if_not_exists(folder_path): def move_it(source_path, destination_dir, overwrite=False): try: - + # Ensure destination directory exists create_folder_if_not_exists(destination_dir) filename = os.path.basename(source_path) destination_path = os.path.join(destination_dir, filename) - if os.path.exists(destination_path) and overwrite: - base, ext = os.path.splitext(filename) - i = 1 - while True: - new_filename = f"{base}_{i}{ext}" - new_destination_path = os.path.join( - destination_dir, new_filename) - if not os.path.exists(new_destination_path): - destination_path = new_destination_path - break - i += 1 - - shutil.move(source_path, destination_path) - # print(f"Image moved from '{source_path}' to '{destination_path}'") + + # Check if the destination file exists + if os.path.exists(destination_path): + if overwrite: + # If overwrite is True, move the file directly + shutil.move(source_path, destination_path) + else: + # If overwrite is False, find a new filename with an incremented counter + base, ext = os.path.splitext(filename) + i = 1 + while True: + new_filename = f"{base}_{i}{ext}" + new_destination_path = os.path.join(destination_dir, new_filename) + if not os.path.exists(new_destination_path): + destination_path = new_destination_path + break + i += 1 + shutil.move(source_path, destination_path) + else: + # If the destination file does not exist, move the file directly + shutil.move(source_path, destination_path) + except Exception as e: - print("") - # print(f"Error: {e}") + print(f"Error: {e}") def create_folder_if_not_exists(folder_path): diff --git a/JDCN_VHSFileMover.py b/JDCN_VHSFileMover.py index 6dfc23b..8eb0ef6 100644 --- a/JDCN_VHSFileMover.py +++ b/JDCN_VHSFileMover.py @@ -10,27 +10,35 @@ def create_folder_if_not_exists(folder_path): def move_it(source_path, destination_dir, overwrite=False): try: - + # Ensure destination directory exists create_folder_if_not_exists(destination_dir) filename = os.path.basename(source_path) - destination_path = os.path.join(destination_dir, filename) - if os.path.exists(destination_path) and overwrite: - base, ext = os.path.splitext(filename) - i = 1 - while True: - new_filename = f"{base}_{i}{ext}" - new_destination_path = os.path.join(destination_dir, new_filename) - if not os.path.exists(new_destination_path): - destination_path = new_destination_path - break - i += 1 - - shutil.move(source_path, destination_path) - # print(f"Image moved from '{source_path}' to '{destination_path}'") + destination_path = os.path.join(destination_dir, filename) + + # Check if the destination file exists + if os.path.exists(destination_path): + if overwrite: + # If overwrite is True, move the file directly + shutil.move(source_path, destination_path) + else: + # If overwrite is False, find a new filename with an incremented counter + base, ext = os.path.splitext(filename) + i = 1 + while True: + new_filename = f"{base}_{i}{ext}" + new_destination_path = os.path.join(destination_dir, new_filename) + if not os.path.exists(new_destination_path): + destination_path = new_destination_path + break + i += 1 + shutil.move(source_path, destination_path) + else: + # If the destination file does not exist, move the file directly + shutil.move(source_path, destination_path) + except Exception as e: - print("") - # print(f"Error: {e}") + print(f"Error: {e}") class JDCN_VHSFileMover: