Wednesday, March 22, 2017

Excel VBA- FSO Rename Files By Their Sizes

By Ajeet Kumar

Excel VBA To Rename Files By Their Sizes

I have a number of MP4 files in a folder. I want to rename each file by their file size so that they could appear together ordered by size. The following code is helpful to do this.



Sub pRenameFileBySize2()
    Dim objFS As Object
    Dim objF As Object
    Dim lngC As Long
    Dim strExtension As String
    Dim strOldName As String
    Dim strNewName As String
   
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Dim varPath
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Title = "Choose The Folder"
        .Show
        If .SelectedItems.Count < 1 Then
            Exit Sub
        Else
            varPath = .SelectedItems(1)
        End If
    End With
'    On Error Resume Next
    lngC = 999
    For Each objF In objFS.GetFolder(varPath).Files
        lngC = lngC + 1
'        strOldName = objF.Name
'        strExtension = Split(strOldName, ".")(UBound(Split(strOldName, ".")))
        strNewName = "V-" & objF.Size & "-" & lngC & "." & "mp4"
        objF.Name = strNewName
    Next objF
    Set objFS = Nothing
End Sub

No comments:

Post a Comment

Hot Topics