|
本帖最后由 ganlinlao 于 2024-7-19 10:23 编辑
@Roych 你的name语句代码应该是有瑕疵。
刚好也遇到需要 vba代码 批量修改文件名。
递归无限级子文件夹,批量替换文件名。
Sub main()
RenameFiles Application.ActiveDocument.Path
End Sub
Sub RenameFiles(folderPath As String)
'引用 microsoft scripting runtime
Dim fso As New Scripting.FileSystemObject, myfolder As Folder, subFolder As Folder
Dim myFile As File, fileName As String, newName As String
' 循环遍历文件夹内的所有文件
For Each myFile In fso.GetFolder(folderPath).Files
' 获取文件名
fileName = myFile.Name
If InStr(1, fileName, "免费") > 0 Then
' 根据需求修改文件名
newName = Replace(fileName, "【学习资源库】免费分享", "")
' 修改文件名
Name folderPath & "\" & fileName As folderPath & "\" & newName
'不喜欢用Name语句,用下一行代码也可以。
'myFile.Name = newName
End If
Next myFile
'递归子文件夹,批量处理
For Each subFolder In fso.GetFolder(folderPath).subfolders
RenameFiles subFolder.Path
Next
' 释放对象
Set fso = Nothing
End Sub
|
|