I have a bunch of files:
File Completed for 123456 1 - Platform Junk (AP .msg
File Completed for 1234566 1 - More Junk here and Stuf.msg
File Completed for 654321 1 - ® Stuff and Junk.msg
So each file contains a 6 or 7 digit number (not including that 1 after the number), also some files have stupid R (registered trademark) symbols.
My goal is to search the current directory for all .msg files, find the 6 or 7 digit number, and rename the file to 123456.msg or 11234567.msg`.
I have the regex that should work properly to extract the number:
(?<!\d)(\d{6}|\d{7})(?!\d)
Now I just need to loop through all .msg files and rename them. I've got my foot in the door with the following code, but I don't quite know how to extract what I want and rename:
for filename in glob.glob(script_dir + '*.msg'):
new_name = re.sub(r'(?<!\d)(\d{6}|\d{7})(?!\d)')
Any help or step in the right direction would be much appreciated!