file_move.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. from datetime import datetime
  3. from pathlib import Path
  4. blacklist = ["_\\", "_ori", "_.csv", "Kopie", "copy", "_sich", "Plan"]
  5. def move(system_dir: str):
  6. max_age_ts = datetime.now().timestamp() - 24 * 60 * 60
  7. ignore_file = Path(f"{system_dir}\\IQD\\ignoriert.txt")
  8. ignore_list = []
  9. if ignore_file.exists():
  10. ignore_list = ignore_file.read_text(encoding="latin-1").split("\n")
  11. no_files = True
  12. for source in Path(f"{system_dir}\\IQD").rglob("*.csv"):
  13. if source.name in ignore_list:
  14. continue
  15. if ignore_any_parent_folder(source, ignore_list):
  16. continue
  17. full_file = str(source)
  18. blacklisted = [pattern in full_file for pattern in blacklist]
  19. if any(blacklisted):
  20. continue
  21. no_files = False
  22. print("* " + str(source))
  23. dest = Path(f"{system_dir}\\Export\\{source.name}")
  24. source_size = source.stat().st_size
  25. source_ts = source.stat().st_mtime
  26. if source_size <= 20:
  27. print(f"!! Datei {source.name} ist leer !!")
  28. continue
  29. if source_ts < max_age_ts:
  30. print(f"!! Datei {source.name} ist aelter als 24 Stunden !!")
  31. continue
  32. if dest.exists():
  33. dest_size = dest.stat().st_size
  34. if source_size < dest_size // 10:
  35. print(f"!! Datei {source.name} ist zu klein !!")
  36. print(f"{source}: {source_size // 1024} KB")
  37. print(f"{dest}: {dest_size // 1024} KB")
  38. print("")
  39. continue
  40. dest_ts = dest.stat().st_mtime
  41. if source_ts < dest_ts:
  42. print(f"!! Datei {source.name} ist aelter als die Zieldatei !!")
  43. print(str(source) + ": " + datetime.fromtimestamp(source_ts).strftime("%d.%m.%Y, %H:%M:%S"))
  44. print(str(dest) + ": " + datetime.fromtimestamp(dest_ts).strftime("%d.%m.%Y, %H:%M:%S"))
  45. print("")
  46. continue
  47. dest.unlink()
  48. os.rename(source, dest)
  49. return no_files
  50. def ignore_any_parent_folder(source: Path, ignore_list: list[str]):
  51. for p in source.parents:
  52. if p.name + "\\" in ignore_list:
  53. return True
  54. return False
  55. def check(system_dir: str):
  56. max_age_ts = datetime.now().timestamp() - 24 * 60 * 60
  57. ignore_file = Path(f"{system_dir}\\Export\\ignoriert.txt")
  58. ignore_list = []
  59. if ignore_file.exists():
  60. ignore_list = ignore_file.read_text(encoding="latin-1").split("\n")
  61. clean_exit = True
  62. for dest in Path(f"{system_dir}\\Export").glob("*.csv"):
  63. if dest.name in ignore_list:
  64. continue
  65. dest_ts = dest.stat().st_mtime
  66. if dest_ts < max_age_ts:
  67. print(f"!! Datei {dest.name} ist aelter als 24 Stunden !!")
  68. print(str(dest) + ": " + datetime.fromtimestamp(dest_ts).strftime("%d.%m.%Y, %H:%M:%S"))
  69. clean_exit = False
  70. continue
  71. return clean_exit