Add sorter

main
jollyz88 2024-08-08 07:30:07 +00:00
commit ce1e00f220
1 changed files with 113 additions and 0 deletions

113
sorter Normal file
View File

@ -0,0 +1,113 @@
import sys
import os
from datetime import datetime
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QLabel, QFileDialog, QProgressBar
from PySide6.QtCore import Qt, QThread, Signal
class FileOrganizer(QThread):
progress = Signal(int)
finished = Signal()
def __init__(self, path):
super().__init__()
self.path = path
def run(self):
files = [f for f in os.listdir(self.path) if os.path.isfile(os.path.join(self.path, f))]
total_files = len(files)
for i, file in enumerate(files):
file_path = os.path.join(self.path, file)
_, extension = os.path.splitext(file)
extension = extension[1:].lower() # Rimuove il punto e converte in minuscolo
# Crea la cartella per l'estensione se non esiste
ext_folder = os.path.join(self.path, extension)
if not os.path.exists(ext_folder):
os.makedirs(ext_folder)
# Ottiene la data di creazione del file
creation_time = os.path.getctime(file_path)
date_folder = datetime.fromtimestamp(creation_time).strftime('%Y-%m-%d')
# Crea la sottocartella per la data se non esiste
date_folder_path = os.path.join(ext_folder, date_folder)
if not os.path.exists(date_folder_path):
os.makedirs(date_folder_path)
# Sposta il file nella cartella appropriata
new_file_path = os.path.join(date_folder_path, file)
os.rename(file_path, new_file_path)
# Aggiorna il progresso
self.progress.emit(int((i + 1) / total_files * 100))
self.finished.emit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("File Organizer")
self.setGeometry(100, 100, 400, 200)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
# Path input
path_layout = QHBoxLayout()
self.path_input = QLineEdit()
path_layout.addWidget(QLabel("Percorso:"))
path_layout.addWidget(self.path_input)
browse_button = QPushButton("Sfoglia")
browse_button.clicked.connect(self.browse_folder)
path_layout.addWidget(browse_button)
layout.addLayout(path_layout)
# Organize button
self.organize_button = QPushButton("Organizza File")
self.organize_button.clicked.connect(self.organize_files)
layout.addWidget(self.organize_button)
# Progress bar
self.progress_bar = QProgressBar()
layout.addWidget(self.progress_bar)
# Status label
self.status_label = QLabel()
layout.addWidget(self.status_label)
def browse_folder(self):
folder = QFileDialog.getExistingDirectory(self, "Seleziona Cartella")
if folder:
self.path_input.setText(folder)
def organize_files(self):
path = self.path_input.text()
if not path or not os.path.isdir(path):
self.status_label.setText("Percorso non valido")
return
self.organize_button.setEnabled(False)
self.status_label.setText("Organizzazione in corso...")
self.progress_bar.setValue(0)
self.organizer = FileOrganizer(path)
self.organizer.progress.connect(self.update_progress)
self.organizer.finished.connect(self.organization_finished)
self.organizer.start()
def update_progress(self, value):
self.progress_bar.setValue(value)
def organization_finished(self):
self.organize_button.setEnabled(True)
self.status_label.setText("Organizzazione completata!")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())