# Filename: BRN. py
# Description: Batch replace certain words in file names
# Use to bat rename the file in a Dir (modify the Suffix from A to B) for Windows Vista OS
Import sys
Import OS
Import fnmatch
Import re
# Parse Params
P = input ("Please input work directory (current path for enter ):")
If P = '/R ':
P = '.'
P = P. rstrip ('/R ')
Print (P)
While not OS. Path. exists (P ):
Print (p + 'is not existed. Please input the work Directory :')
P = input ("Please input work directory (current path for enter ):")
S = input ("Please enter the words which need be modified (must ):")
While S = '/R ':
S = input ("Please enter the words which need be replaced (must ):")
S = S. rstrip ('/R ')
D = input ("Please enter the words which want to change to (must ):")
While d = '/R ':
D = input ("Please enter the words which want to change to (must ):")
D = D. rstrip ('/R ')
Try:
Sure = input ("are you sure to rename the file named *" + S + "*" + "to *" + D + "*" + "in directory" + P + "? Y/N :")
Sure = Sure. rstrip ('/R ')
If sure! = 'Y ':
Print ("cancel ")
Else:
For root, dirs, files in OS. Walk (p, true ):
For file in files:
Print (OS. Path. Join (root, file ))
If OS. Path. isfile (OS. Path. Join (root, file): # Only file is file, not a dir, do this
If fnmatch. fnmatch (file, '*' + S + '*'):
F = STR (file). Replace (S, d)
If P = '.':
Command = 'move '+ STR (File) + "" + F
Else:
Command = "move" + OS. Path. Join (root, file) + "" + OS. Path. Join (root, F)
Print (command)
If OS. System (command) = 0: # Do actual rename
Print ("RENAME" + STR (File) + "to" + F + "success ")
Else:
Print ("RENAME" + STR (File) + "to" + F + "failed ")
# Else:
# Print STR (File) + "is a directory. Omit"
Failed t indexerror:
Print (indexerror. Message)