VBA修改文件扩展名

本文介绍了如何通过Visual Basic for Applications (VBA)编写宏,实现批量修改文件的扩展名。内容涵盖VBA的基本语法,以及如何遍历指定文件夹并更改文件后缀的操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dim result As Integer

Private Sub CommandButton2_Click()
    
    Dim first As String
    Dim second As String
    
    first = range("I13")
    second = range("K13")
    
    result = 0
    
    Call getAllDir("g:\test\", first, second)
    
    range("I14") = "修改完成,共修改" & result & "件"
    
End Sub

Private Sub getAllDir(dirs As String, oldExt As String, newExt As String)

    Dim Tochange As String
    Dim sf As String
    Dim allSun() As String
    Dim i As Integer
    
    i = 1
    Tochange = Dir(dirs)
    Call changeName(dirs, oldExt, newExt)
    sf = Dir(dirs, vbDirectory)
    Do While sf <> ""
        If sf <> "." And sf <> ".." Then
            If (GetAttr(dirs & sf) And vbDirectory) = vbDirectory Then
                ReDim Preserve allSun(1 To i) As String
                allSun(i) = dirs & sf & "\"
                i = i + 1
            End If
        End If
        sf = Dir
    Loop
    If (CStr(Join(allSun, ""))) = "" Then
        GoTo NoCall
    End If
    finish = UBound(allSun)
    For j = 1 To finish
        Call getAllDir(allSun(j), oldExt, newExt)
    Next
NoCall:
End Sub

Private Sub changeName(dirs As String, oldExt As String, newExt As String)

    Dim Tochange As String
    Dim TheLen As Integer
    Dim Newi As String
    Dim Oldi As String
    
    
    Tochange = Dir(dirs)
    Do While Tochange <> ""
        TheLen = Len(Tochange)
        Oldi = dirs & Tochange
        If Right(Tochange, Len(oldExt)) <> oldExt Then
            GoTo NextLoop
        End If
        Tochange = Left(Tochange, TheLen - Len(oldExt))
        Newi = dirs & Tochange & newExt
        Name Oldi As Newi
        result = result + 1
NextLoop:
        Tochange = Dir
    Loop

End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值