Increment file counter suffix as much as needed to get a unique file name

This commit is contained in:
hygienic-books 2022-03-24 23:23:58 +01:00
parent af1525a2c3
commit 91370068e4

View File

@ -286,14 +286,16 @@ def move_file_to_target_dir(
target_ext = season_ep_str["ext"] target_ext = season_ep_str["ext"]
target_file_name_plus_ext = f"{target_file_name}{target_ext}" target_file_name_plus_ext = f"{target_file_name}{target_ext}"
if target_file_name_plus_ext in target_file_list: while target_file_name_plus_ext in target_file_list:
log.debug(f"Intended file name already exists in target dir, incrementing counter ...") log.debug(f"Intended file name already exists in target dir, incrementing counter suffix ...")
episode_minus_counter = target_file_name[:-2] episode_minus_counter = target_file_name[:-2]
counter = target_file_name[-2:] counter = target_file_name[-2:]
counter_length = len(counter) counter_length = len(counter)
counter_stripped = int(counter.lstrip("0")) counter_stripped = int(counter.lstrip("0"))
counter_stripped += 1 counter_stripped += 1
target_file_name = f"{episode_minus_counter}{str(counter_stripped).zfill(counter_length)}" target_file_name = f"{episode_minus_counter}{str(counter_stripped).zfill(counter_length)}"
target_file_name_plus_ext = f"{target_file_name}{target_ext}"
target_abs_path = os.path.join(target_dir, f"{target_file_name}{target_ext}") target_abs_path = os.path.join(target_dir, f"{target_file_name}{target_ext}")
try: try: