import os import shutil def copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id): # Construct the path for the copied folder copied_folder_path = os.path.join(target_folder, new_folder_name) # Check if the target folder already exists if os.path.exists(copied_folder_path): print(f"Folder '{copied_folder_path}' already exists. Skipping.") return # Copy the source folder to the target folder 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/IFR/Products" # new_folder_name = "AP_LRDSPE_2024_0001" content_id = "ALL_DT_MAT" folder_name_list = [ "IFR_BJL_2023_ALL_DT_MAT_0001", "IFR_BJT_2023_ALL_DT_MAT_0001", "IFR_BTPC_2023_ALL_DT_MAT_0001", "IFR_BTE_2024_ALL_DT_MAT_0001", "IFR_BTPE_2023_ALL_DT_MAT_0001", "IFR_BTEC_2023_ALL_DT_MAT_0001", "IFR_BTT_2024_ALL_DT_MAT_0001", "IFR_CPAC_2024_ALL_DT_MAT_0001", "IFR_CPAX_2023_ALL_DT_MAT_0002", "IFR_CPE_2024_ALL_DT_MAT_0001", "IFR_CPT_2024_ALL_DT_MAT_0001", "IFR_GTAF_2023_ALL_DT_MAT_0001", "IFR_GTC_2023_ALL_DT_MAT_0001", "IFR_GTL_2023_ALL_DT_MAT_0001", "IFR_GTHS_2023_ALL_DT_MAT_0001", "IFR_GTS_2023_ALL_DT_MAT_0001", "IFR_NSMP_2023_ALL_DT_MAT_0001", ] # loop through list, and remove _ALL_DT_MAT from the names for folder_name in folder_name_list: new_folder_name = folder_name.replace("_ALL_DT_MAT", "") copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id) # copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)