Copy Code code as follows:
#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 to be modified (must):")
While s== ' \ R ':
S=input ("Please enter the words which need to be replaced (must):")
S=s.rstrip (' \ R ')
D=input ("Please enter the words which want to" (MUST): ")
While d== ' \ R ':
D=input ("Please enter the words which want 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, the 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, does 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"
Except Indexerror:
Print (Indexerror.message)