放到Excel VB编辑器里面运行,分为两部分,一个是读取文件名称,一个是修改文件名称,运行两次就行,自己在运行第一次时需要手动添加修改后的图片名称(记得加后缀名称),修改完成后运行第二个VB程序。
[Visual Basic] 纯文本查看 复制代码 Sub 批量获取文件名()
Cells = ""
Dim sfso
Dim myPath As String
Dim Sh As Object
Dim Folder As Object
Application.ScreenUpdating = False
On Error Resume Next
Set sfso = CreateObject("Scripting.FileSystemObject")
Set Sh = CreateObject("shell.application")
Set Folder = Sh.BrowseForFolder(0, "", 0, "")
If Not Folder Is Nothing Then
myPath = Folder.Items.Item.Path
End If
Application.ScreenUpdating = True
Cells(1, 1) = "旧版名称"
Cells(1, 2) = "文件类型"
Cells(1, 3) = "所在位置"
Cells(1, 4) = "新版名称"
Call 直接提取文件名(myPath & "\")
End Sub
'获取选定文件夹下的所有表格名称
Sub 直接提取文件名(myPath As String)
Dim i As Long
Dim myTxt As String
i = Range("A1048576").End(xlUp).Row
myTxt = Dir(myPath, 31)
Do While myTxt <> ""
On Error Resume Next
If myTxt <> ThisWorkbook.Name And myTxt <> "." And myTxt <> ".." And myTxt <> "081226" Then
i = i + 1
Cells(i, 1) = "'" & myTxt
If (GetAttr(myPath & myTxt) And vbDirectory) = vbDirectory Then
Cells(i, 2) = "文件夹"
Else
Cells(i, 2) = "文件"
End If
Cells(i, 3) = Left(myPath, Len(myPath) - 1)
End If
myTxt = Dir
Loop
End Sub
'批量重新命名表格名称
Sub 批量重命名()
Dim y_name As String
Dim x_name As String
For i = 2 To Range("A1048576").End(xlUp).Row
y_name = Cells(i, 3) & "\" & Cells(i, 1)
x_name = Cells(i, 3) & "\" & Cells(i, 4)
On Error Resume Next
Name y_name As x_name
Next
End Sub |