


# https://www.globalsino.com/ICs/
# Copy the corresponding CSV files into these temporary folders


import os
import tempfile
import shutil

realfolder = r"C:\GlobalSino20230219\ICsSupport\BoxAndWhiskerPlot\DataII" 

# Create a dictionary to store files in temporary folders
tempFolders = {}

# Iterate through files in the folder
for filename in os.listdir(realfolder):
    if filename.endswith(".csv"):
        # Extract the common ending portion between the last "-" and ".csv" extension
        parts = filename.split("-")
        if len(parts) >= 2:
            CommonEnding = parts[-2]
        else:
            CommonEnding = "No such files"  

        # Create a temporary folder if it doesn't exist already
        if CommonEnding not in tempFolders:
            tempDir = tempfile.mkdtemp()
            tempFolders[CommonEnding] = tempDir

        # Copy the file to the corresponding temporary folder
        source_path = os.path.join(realfolder, filename)
        dest_path = os.path.join(tempFolders[CommonEnding], filename)
        shutil.copy(source_path, dest_path)

# List how many temporary folders were created
num_tempFolders = len(tempFolders)
print(f"Number of temporary folders created: {num_tempFolders}")

# Print the paths of the temporary folders
for CommonEnding, tempDir in tempFolders.items():
    print(f"Temporary folder '{CommonEnding}': {tempDir}")

    # List the files in the temporary folder
    temp_files = os.listdir(tempDir)
    print(f"Files in '{CommonEnding}' temporary folder:")
    for temp_file in temp_files:
        print(temp_file)
