# -*- coding: utf-8 -*-
import os
 
class BatchRename():
    '''
    批量重命名文件夹中的文件
    '''
    def __init__(self):
        self.path = 'E:/Workspace/aurora'  #表示需要命名处理的文件夹
        self.path= os.getcwd()
        self.srcName='Aurora.Micorservices'
        self.destName='Aurora.Microservices'
 
    def rename(self):
        filelist = os.listdir(self.path)      #获取文件路径
        total_num = len(filelist)     #获取文件长度（个数）
        i=0
        for item in filelist:
            if item.startswith(self.srcName):  # 找到 [srcName] 开头的文件夹或文件
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path),item.replace(self.srcName,self.destName)) # 替换名字
                try:
                    os.rename(src, dst)
                    print ('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except:
                    continue
        print ('total %d to rename & converted %d ,%s-to-%s' % (total_num, i,self.srcName,self.destName))
 
    def rename_all(self,path):
        filelist = os.listdir(path)      #获取文件路径
        total_num = len(filelist)     #获取文件长度（个数）
        i=0
        for item in filelist:
            src = os.path.join(os.path.abspath(path), item)
            if os.path.isdir(src):  # 如果是文件夹进入递归
                self.rename_all(src)
            if item.startswith(self.srcName):  # 找到 [srcName] 开头的文件夹或文件
                dst = os.path.join(os.path.abspath(path),item.replace(self.srcName,self.destName)) # 替换名字
                try:
                    os.rename(src, dst)
                    print ('converting %s to %s ...' % (src, dst))
                    i = i + 1
                except:
                    continue
        print ('total %d to rename & converted %d, %s-to-%s' % (total_num, i,self.srcName,self.destName))
    
if __name__ == '__main__':
    demo = BatchRename()
    #demo.rename()
    demo.rename_all(demo.path)