import os import shutil def copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id): # Copy the source folder to the target folder copied_folder_path = os.path.join(target_folder, new_folder_name) shutil.copytree(source_folder, copied_folder_path) # Extract components from the new folder name components = new_folder_name.split("_") if len(components) != 4: raise ValueError( "new_folder_name must be in the format Brand_AssetName_Year_AssetNumber" ) brand = components[0] asset_name = components[1] year = components[2] asset_number = components[3] # Construct the new .blend file name new_blend_file_name = ( f"{brand}_{asset_name}_{year}_{content_id}_{asset_number}.blend" ) # Rename the .blend file inside the copied folder blend_folder_path = os.path.join(copied_folder_path, "BLEND") for file_name in os.listdir(blend_folder_path): if file_name.endswith(".blend"): old_blend_file_path = os.path.join(blend_folder_path, file_name) new_blend_file_path = os.path.join(blend_folder_path, new_blend_file_name) os.rename(old_blend_file_path, new_blend_file_path) break print(f"Copied and renamed folder to {copied_folder_path}") print(f"Renamed .blend file to {new_blend_file_name}") # Example usage source_folder = ( r"Z:/01_Production_AP/01_Library/03_Elements/_Brand_AssetName_Year_AssetNumber" ) target_folder = r"Z:/01_Production_AP/01_Library/03_Elements/AP/Products" new_folder_name = "AP_LRDSPE_2024_0001" content_id = "ALL_DT_MAT" copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)