| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import csv
- import os
- import shutil
- from pathlib import Path
- def default_desktop() -> Path:
- user_profile = Path(os.environ.get("USERPROFILE", str(Path.home())))
- return user_profile / "Desktop"
- def read_csv(csv_path: Path) -> list[dict[str, str]]:
- if not csv_path.is_file():
- raise ValueError(f"Die CSV-Datei existiert nicht: {csv_path}")
- with csv_path.open("r", newline="", encoding="latin-1") as csv_file:
- return list(csv.DictReader(csv_file, delimiter=";"))
- def move_files(
- files_csv: Path,
- clients_csv: Path,
- desktop_directory: Path | None = None,
- ) -> tuple[int, int, int]:
- desktop_directory = desktop_directory or default_desktop()
- client_rows = read_csv(clients_csv)
- client_names = {
- row.get("ParticipantName", "").strip(): row.get("Kunde", "").strip()
- for row in client_rows
- if row.get("ParticipantName", "").strip() and row.get("Kunde", "").strip()
- }
- moved_count = 0
- skipped_count = 0
- missing_client_count = 0
- for row in read_csv(files_csv):
- participant_name = row.get("ParticipantName", "").strip()
- source = Path(row.get("FilePath", "").strip())
- customer = client_names.get(participant_name)
- if not customer:
- missing_client_count += 1
- continue
- if not source.is_file():
- skipped_count += 1
- continue
- target_directory = desktop_directory / "PC-Visit" / customer
- target_directory.mkdir(parents=True, exist_ok=True)
- target = target_directory / source.name
- if source.resolve() == target.resolve() or target.exists():
- skipped_count += 1
- continue
- shutil.move(str(source), str(target))
- print(f"Datei verschoben: {source} -> {target}")
- moved_count += 1
- return moved_count, skipped_count, missing_client_count
- def refresh_and_move(
- directory: Path,
- files_csv: Path,
- clients_csv: Path,
- desktop_directory: Path | None = None,
- ignore_timestamp: bool = False,
- ) -> tuple[int, int, int, int, int]:
- from files import run as run_files
- xml_count, file_count = run_files(directory, files_csv, ignore_timestamp=ignore_timestamp)
- moved_count, skipped_count, missing_client_count = move_files(files_csv, clients_csv, desktop_directory)
- return xml_count, file_count, moved_count, skipped_count, missing_client_count
|