archive_scratch.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. #ident "@(#)$Format:LocalFoodAI_lanfr144:archive_scratch.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$"
  3. import os
  4. import shutil
  5. def archive_scratch():
  6. # Define directories
  7. base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  8. scratch_dir = os.path.join(base_dir, "scratch")
  9. user_profile = os.environ.get("USERPROFILE") or os.path.expanduser("~")
  10. keep_dir = os.path.join(user_profile, "keep")
  11. # Create destination folder if not exists
  12. if not os.path.exists(keep_dir):
  13. os.makedirs(keep_dir)
  14. print(f"Created archive directory: {keep_dir}")
  15. # Check scratch directory contents
  16. if not os.path.exists(scratch_dir):
  17. print(f"Scratch directory does not exist: {scratch_dir}")
  18. return
  19. # Move files
  20. files_moved = 0
  21. for filename in os.listdir(scratch_dir):
  22. src_path = os.path.join(scratch_dir, filename)
  23. # Skip directories if any
  24. if not os.path.isfile(src_path):
  25. continue
  26. # Find unique versioned filename
  27. version = 1
  28. while True:
  29. # First file is named test_filter.py;001, then test_filter.py;002...
  30. dest_filename = f"{filename};{version:03d}"
  31. dest_path = os.path.join(keep_dir, dest_filename)
  32. if not os.path.exists(dest_path):
  33. break
  34. version += 1
  35. shutil.move(src_path, dest_path)
  36. print(f"Moved: {filename} -> {dest_path}")
  37. files_moved += 1
  38. print(f"Scratch archiving completed. Total files archived: {files_moved}")
  39. if __name__ == "__main__":
  40. archive_scratch()