Last active
September 4, 2022 00:02
-
-
Save javascripto/767099007add00cd3e73b6b188ef5efe to your computer and use it in GitHub Desktop.
File name sanitizer for FAT partitions that does not accept some characters on filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os, re, argparse | |
| NOT_ALLOWED_CHARACTERS_REGEX = r'["*|/\\:<>?\[\]]' | |
| def parse_arguments(): | |
| parser = argparse.ArgumentParser(description='Rename file or folder with only allowed characters on FAT partitions') | |
| parser.add_argument('initial_directory', type=str, nargs='?', help='Path to find for files and folders with not allowed characters. If not specified, it will only search on current working directory') | |
| parser.add_argument('-r', '--recursive', action='store_true', help='search for not allowed characters in sub directories') | |
| parser.add_argument('--rename', action='store_true', help='if enabled, it renames the files and folders with not allowed characters') | |
| return parser.parse_args() | |
| def sanitize_path(full_path: str) -> str: | |
| original_file_name = os.path.basename(full_path) | |
| sanitized_file_name = re.sub(NOT_ALLOWED_CHARACTERS_REGEX, '', original_file_name) | |
| return full_path.replace(original_file_name, sanitized_file_name) | |
| def base_name_has_not_allowed_characters(full_path: str) -> bool: | |
| file_name = os.path.basename(full_path) | |
| return re.search(NOT_ALLOWED_CHARACTERS_REGEX, file_name) is not None | |
| def rename_file(old_path: str, new_path: str, can_rename: bool) -> None: | |
| print('Old name:', old_path) | |
| print('New name:', new_path, end="\n\n") | |
| if can_rename: | |
| os.rename(old_path, new_path) | |
| def remove_not_allowed_characters(initial_path: str, find_recursively: bool, can_rename: bool): | |
| if os.path.isfile(initial_path) and base_name_has_not_allowed_characters(initial_path): | |
| return rename_file(initial_path, sanitize_path(initial_path), can_rename) | |
| for file_or_folder_name in os.listdir(initial_path): | |
| full_path = os.path.join(initial_path, file_or_folder_name) | |
| if os.path.isdir(full_path) and find_recursively: | |
| remove_not_allowed_characters(full_path, find_recursively, can_rename) | |
| if base_name_has_not_allowed_characters(full_path): | |
| rename_file(full_path, sanitize_path(full_path), can_rename) | |
| def get_initial_path(initial_directory): | |
| if initial_directory is None: return os.getcwd() | |
| return os.path.abspath(os.path.join(os.getcwd(), initial_directory)) | |
| def main(): | |
| arguments = parse_arguments() | |
| initial_path = get_initial_path(arguments.initial_directory) | |
| if not os.path.exists(initial_path): | |
| return print(f'Path "{initial_path}" does not exist') | |
| remove_not_allowed_characters(initial_path, arguments.recursive, arguments.rename) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.