|
楼主 |
发表于 2022-10-11 16:05
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub CommandButton1_Click()
On Error Resume Next
Dim FSO对象 As Object
Dim Sh As Object
Dim Folder As Object
Dim 文件夹 As Object
Dim 子文件夹集合 As Object
Dim 子文件夹 As Object
Dim 当前文件夹 As Object
Dim strPath As String
Dim i As Object
Dim j As Integer
Dim brr()
Set Sh = CreateObject("shell.application")
Set Folder = Sh.BrowseForFolder(0, "选择货品图片:Attachments 文件夹路径", 0, "Attachments")
If Not Folder Is Nothing Then
strPath = Folder.Items.Item.Path
End If
If Len(strPath) = 0 Then
MsgBox "请重新选择货品图片的路径!", vbExclamation + vbOKOnly, "Aryou提示"
Set Sh = Nothing
Set Folder = Nothing
Exit Sub
End If
Set FSO对象 = CreateObject("Scripting.FileSystemobject")
Set 文件夹 = FSO对象.GetFolder(strPath)
Set 子文件夹集合 = 文件夹.SubFolders
For Each 子文件夹 In 子文件夹集合
Set 当前文件夹 = FSO对象.GetFolder(strPath & "\" & 子文件夹.Name)
For Each i In 当前文件夹.Files '子文件名称重命名,会报错已存在
i.Name = Mid(子文件夹.Name, InStr(子文件夹.Name, "-") + 1) & "-" & j & "." & FSO对象.GetExtensionName(i)
j = j + 1
Next
j = 0
brr = 当前文件夹.Files.Name '装入数组
' Dim y As Integer
' y = 0
' For Each i In 当前文件夹.Files
' brr(y) = i.Name '子文件夹的文件名称装入数组
' y = y + 1
' Next
' y = 0
For Each i In 当前文件夹.Files
If i.Name = brr(LBound(brr)) Then '把子文件夹内第一个文件名称更改为子文件名称编号部分
i.Name = Mid(子文件夹.Name, InStr(子文件夹.Name, "-") + 1) & "." & FSO对象.GetExtensionName(i)
End If
Next
Erase brr '清空数组
Next
Set FSO对象 = Nothing
Set 文件夹 = Nothing
Set 子文件夹集合 = Nothing
Set 子文件夹 = Nothing
Set 当前文件夹 = Nothing
Set i = Nothing
Set Sh = Nothing
Set Folder = Nothing
MsgBox "货品图片的名称修改成功!", vbExclamation + vbOKOnly, "Aryou提示"
End Sub
|
|