Mar-27-2025, 01:40 PM
I had a request to help with the process of transferring scanned records from one folder to another and creating subfolders for new records. I was able to get the code working locally on my computer, but it wouldn't work when I sent it to the end user. Would this work running from a client to the server, or does it need to be run on the server? The source and destination were updated to Z drive info for their server.
import os import shutil from datetime import datetime import tkinter as tk from tkinter import messagebox # Source and destination directories source_dir = r'C:\Users\justi\OneDrive\Desktop\scanned' destination_dir = r'F:\scanned' # Function to transfer documents and generate a report def transfer_documents(): # Variables to track the number of files and folders files_transferred = 0 new_folders_created = 0 # Loop through all files in the source directory for filename in os.listdir(source_dir): # Skip directories and process all files regardless of extension if os.path.isfile(os.path.join(source_dir, filename)): # Split the filename to get first name, last name, and date of birth name_parts = filename name_without_extension, _ = os.path.splitext(name_parts) # Remove file extension parts = name_without_extension.split(',') if len(parts) >= 3: # We expect at least three parts: first name, last name, and date of birth first_name = parts[0].strip() last_name = parts[1].strip() dob = parts[2].strip() # Verify that the date of birth is in a valid format (MM-DD-YYYY) try: # Check if the date format is valid datetime.strptime(dob, "%m-%d-%Y") # Construct the folder name based on first name, last name, and date of birth folder_name = f"{first_name}, {last_name}, {dob}" destination_folder = os.path.join(destination_dir, folder_name) # Create the folder in the destination directory if it doesn't exist if not os.path.exists(destination_folder): os.makedirs(destination_folder) new_folders_created += 1 # Construct the full path to the file source_file_path = os.path.join(source_dir, filename) destination_file_path = os.path.join(destination_folder, filename) # Move the file to the new folder shutil.move(source_file_path, destination_file_path) files_transferred += 1 except ValueError: print(f"Skipping {filename}: Invalid date format for date of birth.") # Create a completion report inside the destination directory report_file_path = os.path.join(destination_dir, 'transfer_report.txt') with open(report_file_path, 'w') as report: report.write(f"Transfer Report - {datetime.now()}\n") report.write("="*50 + "\n") report.write(f"Total files transferred: {files_transferred}\n") report.write(f"New folders created: {new_folders_created}\n") report.write("="*50 + "\n") # Popup completion message root = tk.Tk() root.withdraw() # Hide the main window messagebox.showinfo( "Transfer Complete", f"Transfer complete. {files_transferred} files were transferred.\n" f"{new_folders_created} new folders were created.\n" f"Completion report generated at: {report_file_path}" ) root.destroy() # Run the function if __name__ == '__main__': transfer_documents()