Created
September 26, 2018 07:48
-
-
Save timbledum/056ebd2706f34eec8b8cc3ecd8dcfd1d to your computer and use it in GitHub Desktop.
Renamer
This file contains 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
from pathlib import Path | |
import sys | |
import easygui | |
def main(): | |
choices = easygui.multenterbox( | |
msg="This tool replaces part of the file name for all files and folders in the current folder.", | |
title="Renamer", | |
fields=["Old text", "New text"], | |
values=["(e.g., Aug)", "(e.g., Sep)"], | |
) | |
if not choices: | |
sys.exit() | |
old_text, new_text = choices | |
current_dir = Path() | |
files = current_dir.rglob(f"*{old_text}*") | |
file_count = len(list(current_dir.rglob(f"*{old_text}*"))) | |
file_list = "\n - ".join( | |
["Would you like to process the following files/folders?"] | |
+ [str(file) for file in current_dir.rglob(f"*{old_text}*")] | |
) | |
if not easygui.ccbox(file_list): | |
sys.exit() | |
for file in files: | |
new_file_name = file.parent / file.name.replace(old_text, new_text) | |
file.rename(new_file_name) | |
easygui.msgbox(f"Success! {file_count} file(s) updated!") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment