以下是使用Excel VBA将一个文件夹内的文件改名的代码示例:

Sub RenameFilesInFolder()
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim strFolderPath As String
    Dim strNewName As String
    
    ' 设置文件夹路径
    strFolderPath = "C:\YourFolderPath\"
    
    ' 创建文件系统对象
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' 获取文件夹对象
    Set objFolder = objFSO.GetFolder(strFolderPath)
    
    ' 循环遍历文件夹内的文件
    For Each objFile In objFolder.Files
        ' 设置新的文件名
        strNewName = "NewName_" & objFile.Name
        
        ' 改名操作
        objFile.Name = strNewName
    Next objFile
    
    ' 清除对象
    Set objFSO = Nothing
    Set objFolder = Nothing
    Set objFile = Nothing
    
    MsgBox "文件改名完成!"
End Sub

请注意将strFolderPath的值更改为你要修改文件名的文件夹路径。此代码将为文件夹内的每个文件添加前缀"NewName_",你可以根据需要修改strNewName的赋值语句。执行此VBA宏后,它将在完成时显示一个消息框。


原文地址: https://www.cveoy.top/t/topic/i4l9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录