import os
import shutil
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def batch_file_operations(source_dir, dest_dir, operation, file_pattern=None, batch_size=100):
"""
Performs batch operations on files in a source directory and copies them to a destination directory.
For development purposes only, no async logic.
Args:
source_dir (str): Path to the source directory.
dest_dir (str): Path to the destination directory.
operation (str): The operation to perform ('copy', 'rename', 'move', 'delete').
file_pattern (str, optional): File pattern to filter files (e.g., '*.txt'). Defaults to None (all files).
batch_size (int): Number of files to process in each batch. Defaults to 100.
"""
if not os.path.exists(source_dir):
logging.error(f"Source directory '{source_dir}' does not exist.")
return
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
logging.info(f"Created destination directory '{dest_dir}'.")
files = [f for f in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, f))]
if file_pattern:
files = [f for f in files if f.endswith(file_pattern)]
total_files = len(files)
if total_files == 0:
logging.warning("No files found matching the criteria.")
return
for i in range(0, total_files, batch_size):
batch = files[i:i + batch_size]
logging.info(f"Processing batch {i // batch_size + 1} of {total_files // batch_size + (1 if total_files % batch_size else 0)}")
for filename in batch:
source_path = os.path.join(source_dir, filename)
dest_path = os.path.join(dest_dir, filename)
try:
if operation == 'copy':
shutil.copy2(source_path, dest_path) #copy2 preserves metadata
logging.info(f"Copied '{filename}' to '{dest_dir}'.")
elif operation == 'rename':
new_filename = filename + "_renamed"
new_source_path = os.path.join(source_dir, filename)
new_dest_path = os.path.join(dest_dir, new_filename)
os.rename(new_source_path, new_dest_path)
logging.info(f"Renamed '{filename}' to '{new_filename}' in '{dest_dir}'.")
elif operation == 'move':
shutil.move(source_path, dest_path)
logging.info(f"Moved '{filename}' to '{dest_dir}'.")
elif operation == 'delete':
os.remove(source_path)
logging.info(f"Deleted '{filename}' from '{source_dir}'.")
else:
logging.error(f"Invalid operation: '{operation}'")
except Exception as e:
logging.error(f"Error processing '{filename}': {e}")
if __name__ == '__main__':
# Example Usage:
source_directory = "source_files"
destination_directory = "destination_files"
# Create dummy source directory and files for testing
if not os.path.exists(source_directory):
os.makedirs(source_directory)
for i in range(20):
with open(os.path.join(source_directory, f"test_file_{i}.txt"), "w") as f:
f.write(f"This is test file {i}")
batch_file_operations(source_directory, destination_directory, 'copy')
#batch_file_operations(source_directory, destination_directory, 'rename', file_pattern='*.txt')
#batch_file_operations(source_directory, destination_directory, 'move')
#batch_file_operations(source_directory,
Add your comment