MizunoBrasil

Gigacopy 2.8 Copy Files With Progressbar Win32 Dialog

Nov 13th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. import os
  2. import ctypes
  3. from tkinter import *
  4. from tkinter import filedialog, messagebox
  5. from tkinter.ttk import *
  6. from datetime import datetime
  7.  
  8. # Estrutura para a função SHFileOperation
  9. class SHFILEOPSTRUCT(ctypes.Structure):
  10.     _fields_ = [
  11.         ("hwnd", ctypes.c_void_p),
  12.         ("wFunc", ctypes.c_uint),
  13.         ("pFrom", ctypes.c_wchar_p),
  14.         ("pTo", ctypes.c_wchar_p),
  15.         ("fFlags", ctypes.c_uint),
  16.         ("fAnyOperationsAborted", ctypes.c_int),
  17.         ("hNameMappings", ctypes.c_void_p),
  18.         ("lpszProgressTitle", ctypes.c_wchar_p)
  19.     ]
  20.  
  21. # Constantes para o tipo de operação
  22. FO_COPY = 0x0002
  23. FO_MOVE = 0x0001
  24.  
  25. # Flags para controlar o comportamento
  26. FOF_NOCONFIRMATION = 0x0010
  27. FOF_SILENT = 0x0004  # Sem diálogos de erro
  28.  
  29. def native_copy_move(src, dst, move=False):
  30.     operation = FO_MOVE if move else FO_COPY
  31.     file_op = SHFILEOPSTRUCT()
  32.     file_op.wFunc = operation
  33.  
  34.     # Adiciona caracteres nulos no final para compatibilidade com SHFileOperation
  35.     file_op.pFrom = src.replace("/", "\\") + '\0\0'
  36.     file_op.pTo = dst.replace("/", "\\") + '\0\0'
  37.     file_op.fFlags = FOF_NOCONFIRMATION
  38.  
  39.     result = ctypes.windll.shell32.SHFileOperationW(ctypes.byref(file_op))
  40.  
  41.     if result != 0:
  42.         messagebox.showerror("Erro", "Falha na operação de cópia/movimentação.")
  43.     else:
  44.         now = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
  45.         log_entry = f"{now} - {'Movido' if move else 'Copiado'}: {src} -> {dst}"
  46.         log_text.insert(END, log_entry + "\n")
  47.         log_text.see(END)
  48.  
  49. def start_copy_move_native():
  50.     src_paths = [path.strip() for path in src_entry.get().strip().split(";") if path.strip()]
  51.     dst_folder = dst_entry.get().strip()
  52.  
  53.     if not src_paths:
  54.         messagebox.showwarning("Aviso", "É necessário escolher um ou mais arquivos ou pastas para copiar ou mover.")
  55.         return
  56.  
  57.     if not dst_folder or not os.path.exists(dst_folder):
  58.         messagebox.showerror("Erro", "O destino especificado não existe.")
  59.         return
  60.  
  61.     for src in src_paths:
  62.         if os.path.exists(src):
  63.             native_copy_move(src, dst_folder, move=(operation.get() == "move"))
  64.         else:
  65.             # Mensagem removida para evitar alertas desnecessários após uma execução bem-sucedida.
  66.             pass
  67.  
  68.     messagebox.showinfo("Sucesso", f"{'Movido' if operation.get() == 'move' else 'Copiado'} com sucesso!")
  69.     os.startfile(dst_folder)
  70.  
  71. def select_src():
  72.     src_files = filedialog.askopenfilenames()
  73.     src_entry.delete(0, END)
  74.     src_entry.insert(END, ";".join(src_files) + ";")
  75.  
  76. def select_folder():
  77.     folder = filedialog.askdirectory()
  78.     src_entry.insert(END, folder + ";")
  79.  
  80. def select_dst():
  81.     dst_folder = filedialog.askdirectory()
  82.     dst_entry.delete(0, END)
  83.     dst_entry.insert(0, dst_folder)
  84.  
  85. def show_about():
  86.     messagebox.showinfo("Sobre", "GigaCopy 2.8 - Programa para copiar ou mover múltiplos arquivos e pastas usando a barra de progresso padrão do Windows\n21/11/2024, Mizuno")
  87.  
  88. def exit_program():
  89.     window.quit()
  90.  
  91. # Configuração da janela
  92. window = Tk()
  93. window.title("GigaCopy 2.8 com Barra de Progresso Nativa")
  94. window.geometry('710x440')
  95. window.eval('tk::PlaceWindow . center')
  96. window.resizable(False, False)
  97.  
  98. menu_bar = Menu(window)
  99. actions_menu = Menu(menu_bar, tearoff=0)
  100. actions_menu.add_command(label="Adicionar arquivos", command=select_src)
  101. actions_menu.add_command(label="Adicionar pasta", command=select_folder)
  102. actions_menu.add_separator()
  103. actions_menu.add_command(label="Sobre", command=show_about)
  104. actions_menu.add_command(label="Sair", command=exit_program)
  105. menu_bar.add_cascade(label="Ações", menu=actions_menu)
  106. window.config(menu=menu_bar)
  107.  
  108. operation = StringVar(value="copy")
  109.  
  110. Label(window, text="Origem:").grid(row=0, column=0, padx=10, pady=5, sticky=W)
  111. src_entry = Entry(window, width=52)
  112. src_entry.grid(row=0, column=1, padx=10, pady=5, columnspan=2, sticky=W)
  113.  
  114. Label(window, text="Destino:").grid(row=1, column=0, padx=10, pady=5, sticky=W)
  115. dst_entry = Entry(window, width=52)
  116. dst_entry.grid(row=1, column=1, padx=10, pady=5, sticky=W)
  117. Button(window, text="Escolher destino", command=select_dst, width=15).grid(row=1, column=2, padx=5, sticky=W)
  118.  
  119. Label(window, text="Operação:").grid(row=2, column=0, padx=10, pady=5, sticky=W)
  120. Radiobutton(window, text="Copiar", variable=operation, value="copy").grid(row=2, column=1, sticky=W, padx=5)
  121. Radiobutton(window, text="Mover", variable=operation, value="move").grid(row=2, column=1, padx=130, sticky=W)
  122.  
  123. button = Button(window, text="Iniciar", command=start_copy_move_native, width=10)
  124. button.grid(row=3, column=1, padx=10, pady=10, sticky=W)
  125.  
  126. log_text = Text(window, height=15, width=85, state='normal')
  127. log_text.grid(row=4, column=0, columnspan=4, padx=10, pady=10)
  128.  
  129. window.mainloop()
  130.  
Add Comment
Please, Sign In to add comment