import os

import shutil

import pandas as pd

import zipfile

def copy_files(source_folder, destination_folder, file_names):

    file_names = [name.strip() for name in file_names]


    if not os.path.exists(source_folder):

        print("Source folder not found!")

        return

    if not os.path.exists(destination_folder):

        os.makedirs(destination_folder)

        print(f"Destination folder '{destination_folder}' created.")


    for dirpath, _, filenames in os.walk(source_folder):

        for filename in filenames:

            if filename in file_names:

                file_path = os.path.join(dirpath, filename)

                dest_file_path = os.path.join(destination_folder, filename)

                if not os.path.exists(dest_file_path):

                    shutil.copy2(file_path, dest_file_path)  

                    print(f"File '{filename}' copied successfully from '{file_path}'!")


            elif filename.endswith('.zip'):

                zip_path = os.path.join(dirpath, filename)

                extract_and_copy_files(zip_path, destination_folder, file_names)

def extract_and_copy_files(zip_path, destination_folder, file_names):

    try:

        temp_extract_dir = zip_path + "_extracted"

        with zipfile.ZipFile(zip_path, 'r') as zip_ref:

            zip_ref.extractall(temp_extract_dir)

            print(f"Extracted '{zip_path}' to '{temp_extract_dir}'")


        for dirpath, _, filenames in os.walk(temp_extract_dir):

            for filename in filenames:

                if filename in file_names:

                    file_path = os.path.join(dirpath, filename)

                    dest_file_path = os.path.join(destination_folder, filename)

                    if not os.path.exists(dest_file_path):

                        shutil.copy2(file_path, dest_file_path)

                        print(f"File '{filename}' copied successfully from extracted '{file_path}'!")

                    else:

                        print(f"File '{filename}' already exists in the destination folder.")

    except Exception as e:

        print(f"Error extracting '{zip_path}': {e}")

    finally:

        if os.path.exists(temp_extract_dir):

            shutil.rmtree(temp_extract_dir)

            print(f"Temporary extraction directory '{temp_extract_dir}' has been removed.")

def read_file_names_from_excel(excel_file_path):

    """Read the file names from the specified Excel sheet."""

    try:

        df = pd.read_excel(excel_file_path)

        return df['File Name'].dropna().tolist()  

    except Exception as e:

        print(f"Error reading Excel file: {e}")

        return []

if __name__ == "__main__":

    source_folder = r"D:\GILSIL Vs DFCCIL All FIles"

    destination_folder = r"D:\GILSILISSUE1"

    excel_file_path = r"D:\GILSILISSUE1\201ISSUE1.xlsx"  


    file_names = read_file_names_from_excel(excel_file_path)


    if file_names:

        copy_files(source_folder, destination_folder, file_names)

    else:

        print("No file names found in the Excel file.") 

      in this some files are not copied to the destinaiton folder so for those in the excel rpovided add one column missing ..and if it is not copied by any resaon add not dfound for that row and rest will have found