-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_file_copy_ui.py
110 lines (87 loc) · 4.49 KB
/
regex_file_copy_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import re
import shutil
import tkinter as tk
from tkinter import filedialog
# Function to copy directories with or without filtering files based on regex
def copy_directory_structure(src, dest, regex_pattern, invert_filtering):
for root, dirs, files in os.walk(src):
# Create the destination directory structure
rel_path = os.path.relpath(root, src)
dest_dir = os.path.join(dest, rel_path)
# Filter files based on the regex pattern and invert filtering if needed
filtered_files = [file for file in files if (re.match(regex_pattern, file) and not invert_filtering) or (not re.match(regex_pattern, file) and invert_filtering)]
# Copy the files to the destination directory
for file in filtered_files:
src_file = os.path.join(root, file)
dest_file = os.path.join(dest_dir, file)
# Create the destination directory if it doesn't exist
os.makedirs(os.path.dirname(dest_file), exist_ok=True)
shutil.copy2(src_file, dest_file)
def browse_source_directory():
global source_dir
source_dir = filedialog.askdirectory()
source_dir_entry.delete(0, tk.END)
source_dir_entry.insert(0, source_dir)
def browse_destination_directory():
global destination_dir
destination_dir = filedialog.askdirectory()
destination_dir_entry.delete(0, tk.END)
destination_dir_entry.insert(0, destination_dir)
# Function to update the explanation label text
def update_explanation():
invert_filtering = invert_filtering_var.get()
explanation_label.config(text=f"Filtering {'out' if invert_filtering else 'in'} files matching the pattern.")
# Create a tkinter window with a larger size and some basic styling
window = tk.Tk()
window.title("Regex File Copy")
window.geometry("600x350") # Set window size
# Create a frame to contain UI elements for better organization
frame = tk.Frame(window)
frame.pack(padx=20, pady=30)
# Source directory input
source_label = tk.Label(frame, text="Source Directory:",font=("default", 12))
source_label.grid(row=0, column=0, sticky="w")
source_dir_entry = tk.Entry(frame, width=40,font=("default",11))
source_dir_entry.grid(row=0, column=1,pady=5,ipady=5)
source_browse_button = tk.Button(frame, text="Browse", command=browse_source_directory,font=("default", 11))
source_browse_button.grid(row=0, column=2,padx=10)
# Destination directory input
destination_label = tk.Label(frame, text="Destination Directory:",font=("default", 12))
destination_label.grid(row=1, column=0, sticky="w")
destination_dir_entry = tk.Entry(frame, width=40,font=("default",11))
destination_dir_entry.grid(row=1, column=1,pady=5,ipady=5)
destination_browse_button = tk.Button(frame, text="Browse", command=browse_destination_directory,font=("default", 11))
destination_browse_button.grid(row=1, column=2,padx=10)
# Regex pattern input
regex_label = tk.Label(frame, text="Regex Pattern:",font=("default", 12))
regex_label.grid(row=2, column=0, sticky="w")
regex_pattern_entry = tk.Entry(frame, width=40,font=("default",11))
regex_pattern_entry.grid(row=2, column=1,pady=5,ipady=5)
# Checkbox to invert filtering
invert_filtering_var = tk.BooleanVar()
invert_filtering_var.set(False)
invert_filtering_checkbox = tk.Checkbutton(frame, text="Invert Filtering", variable=invert_filtering_var,font=("default", 12), command=update_explanation)
invert_filtering_checkbox.grid(row=3, column=0, columnspan=3, pady=(10, 0), sticky="w")
# Explanation label
explanation_label = tk.Label(frame, text="", fg="blue",font=("default", 14))
explanation_label.grid(row=4, column=0, columnspan=3, pady=(5, 0))
# Copy button
def start_copy():
global source_dir, destination_dir, regex_pattern, invert_filtering
source_dir = source_dir_entry.get()
destination_dir = destination_dir_entry.get()
regex_pattern = regex_pattern_entry.get()
invert_filtering = invert_filtering_var.get()
# Clear the result label text before copying
result_label.config(text="")
copy_directory_structure(source_dir, destination_dir, regex_pattern, invert_filtering)
# Set the result label text after the copy operation
result_label.config(text="Folder structure copied with filtering.")
copy_button = tk.Button(frame, text="Start Copy", command=start_copy,font=("default", 16,"bold"))
copy_button.grid(row=5, column=0, columnspan=3, pady=(20, 0))
# Result label
result_label = tk.Label(frame, text="", fg="green")
result_label.grid(row=6, column=0, columnspan=3)
update_explanation() # Initialize explanation label text
window.mainloop()