commit 19e8c46c3e4727df6441c3eebff2bfb6736f1316 Author: admin Date: Sun May 19 13:05:59 2024 -0400 Todo: If backlash exists in the input, run existing cleaning code. Otherwise, clean directory name with rsplit diff --git a/CountFolderFiles.py b/CountFolderFiles.py new file mode 100644 index 0000000..ff7c9bd --- /dev/null +++ b/CountFolderFiles.py @@ -0,0 +1,66 @@ +import os +import sys + +def append_to_folder_name(folder_path, suffix): + try: + # Get the base folder name + base_folder_name = os.path.basename(folder_path) + + # Append the suffix to the folder name + new_folder_name = f"{base_folder_name}[{suffix}]" + + # Rename the folder + os.rename(folder_path, os.path.join(os.path.dirname(folder_path), new_folder_name)) + print(f"Folder renamed to: {new_folder_name}") + except FileNotFoundError: + print(f"Error: Folder '{folder_path}' not found.") + +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 + except FileNotFoundError: + print(f"Error: Folder '{folder_path}' not found.") + +def check_directory_exists(directory_name): + return os.path.isdir(directory_name) + +def main(): + if len(sys.argv) != 2: + print("Usage: python script.py ") + sys.exit(1) + + directory_name = sys.argv[1] + suffix = count_folder_files(directory_name) + + # If [num] already exists at the end of the string, remove it so we can update it + + # Find the last occurrence of backslash + last_backslash_index = directory_name.rfind('\\') + + # Find the first occurrence of '[' after the last backslash + last_open_bracket_index = directory_name.find('[', last_backslash_index) + + # If '[' exists after the last backslash, remove everything after it + if last_open_bracket_index != -1: + directory_name_cleaned = directory_name[:last_open_bracket_index] + else: + # If '[' doesn't exist, keep the original string + directory_name_cleaned = directory_name + + + if check_directory_exists(directory_name): + print(f"Directory '{directory_name}' exists.") + + #If folder was counted already, remove the old count. Ex: dirname[1] -> dirname + os.rename(directory_name, os.path.join(os.path.dirname(directory_name), directory_name_cleaned)) + directory_name = directory_name_cleaned + + append_to_folder_name(directory_name, suffix) + else: + print(f"Directory '{directory_name}' does not exist.") + +if __name__ == "__main__": + main()