diff --git a/CountFolderFiles.py b/CountFolderFiles.py index b2a42b5..d38a95c 100644 --- a/CountFolderFiles.py +++ b/CountFolderFiles.py @@ -15,12 +15,27 @@ def append_to_folder_name(folder_path, suffix): except FileNotFoundError: print(f"Error: Folder '{folder_path}' not found.") +def count_subdirectories(directory): + subdirectory_count = 0 + for _, dirnames, _ in os.walk(directory): + subdirectory_count += len(dirnames) + return subdirectory_count + def count_folder_files(folder_path): try: - # Count the number of files in the folder - file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file()) - print(f"Number of files in the folder: {file_count}") - return file_count + # If no subfolders exist, return the number of files in the folder + subdirectories = count_subdirectories(folder_path) + if subdirectories == 0: + + # Count the number of files in the folder + file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file()) + print(f"Number of files in the folder: {file_count}") + return file_count + # If subdirectories do exist, return the number of files in each subdirectory then this directory + else: + print(">0") + + except FileNotFoundError: print(f"Error: Folder '{folder_path}' not found.")