|
批量修改文件的文件名
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
Set extension = CreateObject("vbscript.regexp")
With extension
.Global = False
.IgnoreCase = True
.Pattern = "\.\w{2,5}$"
End With
i = Range("A1000000").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
If (GetAttr(myPath & myTxt) And vbDirectory) = vbDirectory Then
Cells(i, 2) = "文件夹"
Cells(i, 1) = "'" & myTxt
Else
Cells(i, 2) = extension.Execute(myTxt)(0)
Cells(i, 1) = "'" & Left(myTxt, Len(myTxt) - Len(Cells(i, 2)))
End If
Cells(i, 3) = Left(myPath, Len(myPath) - 1)
End If
myTxt = Dir
Loop
Columns("A:C").EntireColumn.AutoFit
With Cells(1, 1).CurrentRegion
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
.Interior.PatternColorIndex = xlAutomatic
.Interior.ThemeColor = xlThemeColorDark1
.Interior.TintAndShade = -0.349986266670736
End With
Range(Cells(i, 4), Cells(2, 4)).Interior.Color = vbWhite
End Sub
Sub 批量重命名()
Dim y_name As String
Dim x_name As String
For i = 2 To Range("A1000000").End(xlUp).Row
If Cells(i, 2).Value = "文件夹" Then
y_name = Cells(i, 3) & "\" & Cells(i, 1)
x_name = Cells(i, 3) & "\" & Cells(i, 4)
Else
y_name = Cells(i, 3) & "\" & Cells(i, 1) & Cells(i, 2)
x_name = Cells(i, 3) & "\" & Cells(i, 4) & Cells(i, 2)
End If
On Error Resume Next
Name y_name As x_name
Next
MsgBox ("重命名完成啦")
End Sub |
|