Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- import threading
- import time
- from tkinter import *
- from tkinter.ttk import *
- from tkinter import filedialog, messagebox
- from datetime import datetime
- def start_copy_move():
- src_paths = src_entry.get().split(";")
- dst_folder = dst_entry.get()
- if not src_paths or src_paths == ['']:
- messagebox.showwarning("Aviso", "É necessário escolher um ou mais arquivos ou pastas para copiar ou mover.")
- return
- total_size = sum(
- os.path.getsize(path) if os.path.isfile(path) else sum(
- os.path.getsize(os.path.join(dp, f)) for dp, dn, filenames in os.walk(path) for f in filenames
- ) for path in src_paths
- )
- free_space = shutil.disk_usage(dst_folder).free
- if total_size > free_space:
- messagebox.showerror("Erro", "O destino não possui espaço suficiente para a operação. Libere espaço e tente novamente.")
- return
- log_entries = []
- for src in src_paths:
- if os.path.isfile(src):
- copy_or_move_file(src, dst_folder, log_entries)
- elif os.path.isdir(src):
- copy_or_move_folder(src, dst_folder, log_entries)
- if log_entries:
- with open("gigacopy_log.txt", "a", encoding="utf-8") as log_file:
- for entry in log_entries:
- log_file.write(entry + "\n")
- messagebox.showinfo("Sucesso", f"{'Copiado' if operation.get() == 'copy' else 'Movido'} com sucesso!")
- os.startfile(dst_folder)
- def copy_or_move_file(src, dst_folder, log_entries):
- filename = os.path.basename(src)
- dst = os.path.join(dst_folder, filename)
- GB = os.path.getsize(src) / (1024 * 1024 * 1024)
- copied = 0
- chunk_size = 1024 * 1024 # 1MB
- start_time = time.time()
- with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
- while chunk := fsrc.read(chunk_size):
- fdst.write(chunk)
- copied += len(chunk) / (1024 * 1024 * 1024)
- update_progress(copied, GB, start_time)
- if operation.get() == "move":
- os.remove(src)
- now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
- log_entries.append(f"{now} - {'Copiado' if operation.get() == 'copy' else 'Movido'}: {src} -> {dst}")
- log_text.insert(END, f"{now} - {'Copiado' if operation.get() == 'copy' else 'Movido'}: {src} -> {dst}\n")
- log_text.see(END)
- def copy_or_move_folder(src_folder, dst_folder, log_entries):
- folder_name = os.path.basename(src_folder)
- dst = os.path.join(dst_folder, folder_name)
- if not os.path.exists(dst):
- os.makedirs(dst)
- for root, dirs, files in os.walk(src_folder):
- for file in files:
- file_src = os.path.join(root, file)
- rel_path = os.path.relpath(root, src_folder)
- file_dst = os.path.join(dst, rel_path, file)
- os.makedirs(os.path.dirname(file_dst), exist_ok=True)
- copy_or_move_file(file_src, os.path.dirname(file_dst), log_entries)
- if operation.get() == "move":
- shutil.rmtree(src_folder)
- now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
- log_entries.append(f"{now} - {'Copiado' if operation.get() == 'copy' else 'Movido'}: {src_folder} -> {dst}")
- log_text.insert(END, f"{now} - {'Copiado' if operation.get() == 'copy' else 'Movido'}: {src_folder} -> {dst}\n")
- log_text.see(END)
- def update_progress(copied, GB, start_time):
- progress = (copied / GB) * 100
- bar['value'] = progress
- percent.set(f"{int(progress)}%")
- text.set(f"{round(copied, 2)}/{round(GB, 2)} GB completos")
- elapsed_time = time.time() - start_time
- if copied > 0 and elapsed_time > 0:
- transfer_speed = copied / elapsed_time
- remaining_time = (GB - copied) / transfer_speed
- time_remaining.set(f"Tempo restante: {time.strftime('%H:%M:%S', time.gmtime(remaining_time))}")
- window.update_idletasks()
- def select_src():
- src_files = filedialog.askopenfilenames()
- src_entry.insert(END, ";".join(src_files) + ";")
- def select_folder():
- folder = filedialog.askdirectory()
- src_entry.insert(END, folder + ";")
- def select_dst():
- dst_folder = filedialog.askdirectory()
- dst_entry.delete(0, END)
- dst_entry.insert(0, dst_folder)
- def start_thread():
- threading.Thread(target=start_copy_move).start()
- def show_about():
- messagebox.showinfo("Sobre", "GigaCopy 2.6 - Programa para copiar ou mover múltiplos arquivos e pastas\n31/10/2024, Mizuno")
- def exit_program():
- window.quit()
- # Configuração da janela
- window = Tk()
- window.title("GigaCopy 2.6")
- window.geometry('710x440')
- window.eval('tk::PlaceWindow . center')
- window.resizable(False, False)
- menu_bar = Menu(window)
- actions_menu = Menu(menu_bar, tearoff=0)
- actions_menu.add_command(label="Adicionar arquivos", command=select_src)
- actions_menu.add_command(label="Adicionar pasta", command=select_folder)
- actions_menu.add_separator()
- actions_menu.add_command(label="Sobre", command=show_about)
- actions_menu.add_command(label="Sair", command=exit_program)
- menu_bar.add_cascade(label="Ações", menu=actions_menu)
- window.config(menu=menu_bar)
- percent = StringVar()
- text = StringVar()
- time_remaining = StringVar()
- operation = StringVar(value="copy")
- style = Style()
- style.configure('TButton', padding=(4, 2))
- style.configure('TEntry', padding=(4, 2))
- style.configure('TProgressbar', thickness=20)
- Label(window, text="Origem:").grid(row=0, column=0, padx=10, pady=5, sticky=W)
- src_entry = Entry(window, width=52) # Ajuste de largura para alinhamento uniforme
- src_entry.grid(row=0, column=1, padx=10, pady=5, columnspan=2, sticky=W)
- Label(window, text="Destino:").grid(row=1, column=0, padx=10, pady=5, sticky=W)
- dst_entry = Entry(window, width=52) # Largura igual à caixa de texto "Origem"
- dst_entry.grid(row=1, column=1, padx=10, pady=5, sticky=W)
- Button(window, text="Escolher destino", command=select_dst, width=15).grid(row=1, column=2, padx=5, sticky=W)
- Label(window, text="Operação:").grid(row=2, column=0, padx=10, pady=5, sticky=W)
- Radiobutton(window, text="Copiar", variable=operation, value="copy").grid(row=2, column=1, sticky=W, padx=5)
- Radiobutton(window, text="Mover", variable=operation, value="move").grid(row=2, column=1, padx=130, sticky=W)
- # Barra de progresso alinhada e espaçamento ajustado
- bar = Progressbar(window, orient=HORIZONTAL, mode='determinate', length=510)
- bar.grid(row=3, column=0, columnspan=2, padx=10, pady=(5, 10), sticky=W)
- Label(window, textvariable=percent).grid(row=4, column=0, columnspan=4)
- Label(window, textvariable=text).grid(row=5, column=0, columnspan=4)
- Label(window, textvariable=time_remaining).grid(row=6, column=0, columnspan=4)
- button = Button(window, text="Iniciar", command=start_thread, width=10) # Largura e alinhamento ajustados
- button.grid(row=3, column=2, padx=(0, 10), pady=(5, 10), sticky=W) # Espaço adequado ao lado do botão
- log_text = Text(window, height=8, width=85, state='normal')
- log_text.grid(row=8, column=0, columnspan=4, padx=10, pady=10)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement