I have a program I am writing for an internal employee that takes a CSV file and searches a file server for the files listed in the CSV then copys each file to a folder in the desktop. The issue I am running into with my current code is that the CSV must hold the exact names but instead I need to regex search this and copy the files with file names like the ones in the CSV.
file name in excel looks like: D6957-QR-1452
file name on server looks like: WM_QRLabels_D6957-QR-1452_11.5x11.5_M.pdf
from tkinter import filedialog, messagebox
import openpyxl
import tkinter as tk
from pathlib import Path
import shutil
import os
desktop = Path.home() / "Desktop/Comps"
tk.messagebox.showinfo("Select a directory","Select a directory" )
folder = filedialog.askdirectory()
root = tk.Tk()
root.title("Title")
lbl = tk.Label(
root, text="Open the excel file that includes files to search for")
lbl.pack()
frame = tk.Frame(root)
frame.pack()
scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox = tk.Listbox(frame, yscrollcommand=scrollbar.set)
def load_file():
wb_path = filedialog.askopenfilename(filetypes=[('Excel files', '.xlsx')])
wb = openpyxl.load_workbook(wb_path)
global sheet
sheet = wb.active
listbox.pack()
file_names = [cell.value for row in sheet.rows for cell in row]
for file_name in file_names:
listbox.insert('end', file_name)
return file_names # <--- return your list
def search_folder(folder, file_name):
# Create an empty list to store the found file paths
found_files = []
for root, dirs, files in os.walk(folder):
for file in files:
if file in file_name:
found_files.append(os.path.join(root, file))
shutil.copy2(file, desktop)
return found_files
excelBtn = tk.Button(root, text="Open Excel File",
command=None)
excelBtn.pack()
zipBtn = tk.Button(root, text="Copy to Desktop",
command=search_folder(folder, load_file()))
zipBtn.pack()
root.mainloop()
Program is able to find and copy exact file names but unable to file *like* files.