import os
import shutil
import pandas
import csv

"""
new_dir = 'Inflamed_10x'
directory = 'Cohort2_InflamedFinal'
isExist = os.path.exists(new_dir)
if not isExist:
    os.makedirs(new_dir)
for filename in os.listdir(directory):
    f = os.path.join(directory, filename)
    if f.find("10X") < 0:
        continue
    shutil.move(f, new_dir)

"""
directory = 'Cohort2_InflamedFinal'

with open('csv_data.csv', newline='') as csvfile:
    csv_data = csv.reader(csvfile)
    col_cnt = len(next(csv_data))
    for filename in os.listdir(directory):
        print(filename)
     
1.JPG
102.JPG
104.JPG
105.JPG
111.JPG
116.JPG
121.JPG
128.JPG
13.JPG
132.JPG
139.JPG
150.JPG
163.JPG
170.JPG
176.JPG
179.JPG
182.JPG
2.JPG
201.JPG
202.JPG
210.JPG
217.JPG
218.JPG
22.JPG
221.JPG
225.JPG
229.JPG
236.JPG
238.JPG
241.JPG
25.JPG
250.JPG
256.JPG
266.JPG
276.JPG
279.JPG
28.JPG
280.JPG
298.JPG
30.JPG
300.JPG
305.JPG
320.JPG
330.JPG
331.JPG
341.JPG
345.JPG
346.JPG
35.JPG
351.JPG
355.JPG
365.JPG
386.JPG
387.JPG
51.JPG
57.JPG
59.JPG
60.JPG
64.JPG
71.JPG
74.JPG
75.JPG
76.JPG
78.JPG
90.JPG
98.JPG
import os
import shutil
import pandas
import csv


directory = 'Cohort2_InflamedFinal'


csvfile = open('csv_data.csv', newline='')
for filename in os.listdir(directory):
    csvfile.seek(0)
    csv_data = csv.reader(csvfile)
    # f = os.path.join(directory, filename)
    temp = filename.split(".")
    f = temp[0]
    for csv_row in csv_data:
        # for csv_col_idx in range(0, col_cnt):
        #  print(csv_col_idx, csv_row[csv_col_idx])
        match_temp = csv_row[1]
        match = match_temp[3:len(match_temp)]
        if (match == f):
            new_dir = 'Inflamed_' + csv_row[4].lower()
            isExist = os.path.exists(new_dir)
            if not isExist:
                os.makedirs(new_dir)
            file_with_path = os.path.join(directory, filename)
            shutil.move(file_with_path, new_dir)

            # rename the file
            break
import os
import shutil
import pandas
import csv


directory = 'Cohort2_NonInflamedFinal'


csvfile = open('csv_data.csv', newline='')
for filename in os.listdir(directory):
    csvfile.seek(0)
    csv_data = csv.reader(csvfile)
    # f = os.path.join(directory, filename)
    temp = filename.split(".")
    f = temp[0]
    for csv_row in csv_data:
        # for csv_col_idx in range(0, col_cnt):
        #  print(csv_col_idx, csv_row[csv_col_idx])
        match_temp = csv_row[1]
        match = match_temp[3:len(match_temp)]
        if (match == f):
            new_dir = 'NonInflamed_' + csv_row[4].lower()
            isExist = os.path.exists(new_dir)
            if not isExist:
                os.makedirs(new_dir)
            file_with_path = os.path.join(directory, filename)
            shutil.move(file_with_path, new_dir)

            # rename the file
            break
import os
import shutil
import pandas
import csv
directory = "Inflamed"
directory2 = "_notebooks"
# open the file, and then for each row in the csv file...
csvfile = open('csv_data.csv', newline='')
csv_data = csv.reader(csvfile)
for csv_row in csv_data:
    new_dir_temp = csv_row[0] 
    new_dir = new_dir_temp[0:12]
    isExist = os.path.exists(new_dir)
    if not isExist:
        os.makedirs(new_dir) # make a new directory based on the first 11 characters of column Name


csvfile = open('csv_data.csv', newline='') # open the csv file
for filename in os.listdir(directory): # loop through all the files in Inflamed
    csvfile.seek(0)
    csv_data = csv.reader(csvfile)
    temp = filename.split(".")
    f = temp[0]
    for csv_row in csv_data: 
        match_temp = csv_row[1]
        match = match_temp[3:len(match_temp)]
        if (match == f): # find match for a given file in Inflamed
              actual_name = csv_row[0]
              for cur_file in os.listdir(): # for each file in _notebooks...
                  if os.path.isdir(cur_file):     # if the file is a directory ...        
                    if (actual_name[0:12] == cur_file): # and if the directory is equal to the first 11 characters of the given image file...
                        file_with_path = os.path.join(directory, filename) # create a path for the image file
                        shutil.move(file_with_path, cur_file) # and move the image to the directory that has the matching name