|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import colorlog |
| 4 | + |
| 5 | +# Configure colorlog |
| 6 | +logger = colorlog.getLogger() |
| 7 | +logger.setLevel(colorlog.INFO) # Set the log level |
| 8 | +handler = colorlog.StreamHandler() |
| 9 | +formatter = colorlog.ColoredFormatter( |
| 10 | + "%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s", |
| 11 | + datefmt=None, |
| 12 | + reset=True, |
| 13 | + log_colors={ |
| 14 | + 'DEBUG': 'cyan', |
| 15 | + 'INFO': 'green', |
| 16 | + 'WARNING': 'yellow', |
| 17 | + 'ERROR': 'red', |
| 18 | + 'CRITICAL': 'red,bg_white', |
| 19 | + } |
| 20 | +) |
| 21 | +handler.setFormatter(formatter) |
| 22 | +logger.addHandler(handler) |
| 23 | + |
| 24 | + |
| 25 | +def format_size(size_bytes): |
| 26 | + """Format size into KB, MB, GB""" |
| 27 | + if size_bytes >= 1073741824: # 1 GB |
| 28 | + return f"{size_bytes / 1073741824:.2f} GB" |
| 29 | + elif size_bytes >= 1048576: # 1 MB |
| 30 | + return f"{size_bytes / 1048576:.2f} MB" |
| 31 | + elif size_bytes >= 1024: # 1 KB |
| 32 | + return f"{size_bytes / 1024:.2f} KB" |
| 33 | + else: |
| 34 | + return f"{size_bytes} bytes" |
| 35 | + |
| 36 | + |
| 37 | +def estimate_folder_size(folder_path): |
| 38 | + """Estimate the size of a folder.""" |
| 39 | + total_size = 0 |
| 40 | + for dirpath, dirnames, filenames in os.walk(str(folder_path)): |
| 41 | + for f in filenames: |
| 42 | + fp = os.path.join(str(dirpath), f) |
| 43 | + total_size += os.path.getsize(fp) |
| 44 | + return total_size |
| 45 | + |
| 46 | + |
| 47 | +def copy_folders(source_paths, destination_path): |
| 48 | + """Copy folders to a specified destination.""" |
| 49 | + for source_path in source_paths: |
| 50 | + try: |
| 51 | + shutil.copytree(str(source_path), os.path.join(str(destination_path), os.path.basename(str(source_path)))) |
| 52 | + logger.info(f"Folder '{os.path.basename(source_path)}' copied successfully.") |
| 53 | + except Exception as e: |
| 54 | + logger.error(f"Failed to copy folder '{os.path.basename(source_path)}': {e}") |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + # Get the current user's username |
| 59 | + username = os.getlogin() |
| 60 | + |
| 61 | + # Define the source folders using the current user's username |
| 62 | + source_folders = [ |
| 63 | + f"C:/Users/{username}/Music", |
| 64 | + f"C:/Users/{username}/Pictures", |
| 65 | + f"C:/Users/{username}/Videos" |
| 66 | + ] |
| 67 | + |
| 68 | + # Get the script's directory |
| 69 | + script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 70 | + # Define the destination folder as a DATA folder within the script's directory |
| 71 | + destination_folder = os.path.join(script_dir, "DATA") |
| 72 | + |
| 73 | + # Create the DATA folder if it doesn't exist |
| 74 | + if not os.path.exists(destination_folder): |
| 75 | + os.makedirs(destination_folder) |
| 76 | + logger.info("Created DATA folder.") |
| 77 | + |
| 78 | + # Estimate the sizes of the source folders |
| 79 | + estimated_sizes = {} |
| 80 | + for folder in source_folders: |
| 81 | + if os.path.exists(folder): |
| 82 | + estimated_size = estimate_folder_size(folder) |
| 83 | + formatted_size = format_size(estimated_size) |
| 84 | + estimated_sizes[folder] = formatted_size |
| 85 | + logger.info(f"Estimated size of '{folder}': {formatted_size}") |
| 86 | + else: |
| 87 | + logger.error(f"ERROR: Folder not found: {folder}") |
| 88 | + |
| 89 | + # Proceed with copying the folders without user confirmation |
| 90 | + copy_folders(source_folders, destination_folder) |
| 91 | + logger.info("Folders copied successfully.") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments