怎么批量修改文件名-高级篇
1、准备工作:新建一个空白的Excel表格打开:1.显示 “开发工具” 栏2.启用宏具体操作如下图:注意:如果打开的excel表格有 “开发工具”这一栏并已经启用宏运行的可以跳过这一步。




3、插入一个新模块,并在以下VBA代码复制查姻臂欷黏贴进去:'==========================================Sub 批量获取文件夹里文件名称()myPath 租涫疼迟= "C:\Users\Administrator\Desktop\百度经验\测试" '把文件路径n = 2Cells(1, 1) = "原文件名"Cells(1, 2) = "新文件名"Cells(1, 3) = "实际改后文件名"myFile = Dir(myPath & "\*.*")Do While myFile <> ""Cells(n, 1) = myFilemyFile = Dir '找寻下一个文件n = n + 1LoopEnd SubSub 批量改名()Dim obj As Object '定义obj为变量对象Dim fld, ff '定义fld,ff,gg为变量Dim count!, endL!, index!Dim preFix As String, syfFix As StringpreFix = "" '前缀syfFix = "" '后缀myPath = "C:\Users\Administrator\Desktop\百度经验\测试" '把文件路径Set obj = CreateObject("Scripting.FileSystemObject") '定义变量Set fld = obj.getfolder(myPath)endL = Range("a1000").End(xlUp).Row '获取表A列有数据的总行数For Each ff In fld.Files '遍历文件夹里文件 For count = 2 To endL If Cells(count, 1) = ff.Name Then index = InStr(ff.Name, ".") - 1 '后缀名点所在位置 If index = -1 Then index = Len(ff.Name) End If On Error Resume Next ff.Name = preFix & Cells(count, 2) & syfFix & Right(ff.Name, (Len(ff.Name) - index)) If Err.Number = 58 Then '文件已经存在处理 ff.Name = preFix & Cells(count, 2) & syfFix & "(已经存在同名文件)" & Right(ff.Name, (Len(ff.Name) - index)) End If Cells(count, 3) = ff.Name Exit For End If NextNextFor Each ff In fld.Files For count = 2 To endL If Cells(count, 3) = ff.Name Then ff.Name = Replace(ff.Name, "(已经存在同名文件)", "") Cells(count, 3) = ff.Name '显示修改后的名称 End If NextNextEnd Sub'==========================================

4、修改文件路径:1.将你要修改的文件放在同一个文件夹中2.将VBA代码中的文件路径,修改成你的文件文件夹路径注意:两个地方的文件路径都要改



