Skip to content

Instantly share code, notes, and snippets.

@PaperNick
Created July 8, 2023 14:44
Show Gist options
  • Select an option

  • Save PaperNick/817168972280d0bc45d627e4ec8b848c to your computer and use it in GitHub Desktop.

Select an option

Save PaperNick/817168972280d0bc45d627e4ec8b848c to your computer and use it in GitHub Desktop.

Revisions

  1. PaperNick created this gist Jul 8, 2023.
    21 changes: 21 additions & 0 deletions windows_safe_renamer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    from pathlib import Path


    special_chars = ('<', '>', ':', '"', '/', '\\', '|', '?', '*', '/')


    def replace_all(text, items):
    for i in items:
    text = text.replace(i, '')
    return text


    print('WARNING: Scaning large directories trees may take a lot of time.')

    for file in Path('.').glob('**/*'):
    if any(map(lambda char: char in file.name, special_chars)):
    new_name = replace_all(file.name, special_chars)
    print(f'Old name: {file}')
    print(f'New name: {file.with_name(new_name)}')
    print()
    file.rename(file.with_name(new_name))