1. import os
  2. import shutil
  3. import logging
  4. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  5. def batch_file_operations(source_dir, dest_dir, operation, file_pattern=None, batch_size=100):
  6. """
  7. Performs batch operations on files in a source directory and copies them to a destination directory.
  8. For development purposes only, no async logic.
  9. Args:
  10. source_dir (str): Path to the source directory.
  11. dest_dir (str): Path to the destination directory.
  12. operation (str): The operation to perform ('copy', 'rename', 'move', 'delete').
  13. file_pattern (str, optional): File pattern to filter files (e.g., '*.txt'). Defaults to None (all files).
  14. batch_size (int): Number of files to process in each batch. Defaults to 100.
  15. """
  16. if not os.path.exists(source_dir):
  17. logging.error(f"Source directory '{source_dir}' does not exist.")
  18. return
  19. if not os.path.exists(dest_dir):
  20. os.makedirs(dest_dir)
  21. logging.info(f"Created destination directory '{dest_dir}'.")
  22. files = [f for f in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, f))]
  23. if file_pattern:
  24. files = [f for f in files if f.endswith(file_pattern)]
  25. total_files = len(files)
  26. if total_files == 0:
  27. logging.warning("No files found matching the criteria.")
  28. return
  29. for i in range(0, total_files, batch_size):
  30. batch = files[i:i + batch_size]
  31. logging.info(f"Processing batch {i // batch_size + 1} of {total_files // batch_size + (1 if total_files % batch_size else 0)}")
  32. for filename in batch:
  33. source_path = os.path.join(source_dir, filename)
  34. dest_path = os.path.join(dest_dir, filename)
  35. try:
  36. if operation == 'copy':
  37. shutil.copy2(source_path, dest_path) #copy2 preserves metadata
  38. logging.info(f"Copied '{filename}' to '{dest_dir}'.")
  39. elif operation == 'rename':
  40. new_filename = filename + "_renamed"
  41. new_source_path = os.path.join(source_dir, filename)
  42. new_dest_path = os.path.join(dest_dir, new_filename)
  43. os.rename(new_source_path, new_dest_path)
  44. logging.info(f"Renamed '{filename}' to '{new_filename}' in '{dest_dir}'.")
  45. elif operation == 'move':
  46. shutil.move(source_path, dest_path)
  47. logging.info(f"Moved '{filename}' to '{dest_dir}'.")
  48. elif operation == 'delete':
  49. os.remove(source_path)
  50. logging.info(f"Deleted '{filename}' from '{source_dir}'.")
  51. else:
  52. logging.error(f"Invalid operation: '{operation}'")
  53. except Exception as e:
  54. logging.error(f"Error processing '{filename}': {e}")
  55. if __name__ == '__main__':
  56. # Example Usage:
  57. source_directory = "source_files"
  58. destination_directory = "destination_files"
  59. # Create dummy source directory and files for testing
  60. if not os.path.exists(source_directory):
  61. os.makedirs(source_directory)
  62. for i in range(20):
  63. with open(os.path.join(source_directory, f"test_file_{i}.txt"), "w") as f:
  64. f.write(f"This is test file {i}")
  65. batch_file_operations(source_directory, destination_directory, 'copy')
  66. #batch_file_operations(source_directory, destination_directory, 'rename', file_pattern='*.txt')
  67. #batch_file_operations(source_directory, destination_directory, 'move')
  68. #batch_file_operations(source_directory,

Add your comment