I need to rename pictures in a folder so their new name will include image size and dimensions. Why my code is not working?

Jamualson

Board Regular
Joined
Feb 17, 2021
Messages
145
Office Version
  1. 2019
Platform
  1. Windows
Seemingly the "Debug.Print newName" line is executed 0 times, so the loop is not entered, however there are JPG images and the folder exists. Thank you very much for your idea, I am lost at this one!

VBA Code:
Sub RenameImages()
    Dim fs As FileSystemObject
    Dim folder As folder
    Dim file As file
   
    Set fs = New FileSystemObject
    Set folder = fs.GetFolder("C:\myfolder") ' Replace with the path to your folder
   
    For Each file In folder.Files
        If file.Type Like "JPG Image*" Then
            Dim image As Object
            Set image = CreateObject("WIA.ImageFile")
            image.LoadFile file.Path
           
            ' Compute the new file name
            Dim newName As String
            newName = file.Name & " - " & image.Size & " - " & image.Width & "x" & image.Height
           
            ' Rename the file
            file.Name = newName
            Debug.Print newName
        End If
    Next
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
In Debug mode stop at the line If file.Type Like "JPG Image*" Then and then type ?file.Type in the Immediate pane (Ctrl+G if not visible) and see what you get.
I would use instead:
Code:
If file.Type Like "*JPG*" Then
and also:
VBA Code:
newName = file.Name & " - " & file.Size & " - " & image.Width & "x" & image.Height
 
Upvote 0
Hi - I didn't see that @rollis13 had already responded when I started looking into this. (My apologies, Rollis13.) I agree with the points Rollis13 made above:

1. On my system, JPG Image won't work but JPG File does, so it is worth just looking for *JPG*, as Rollis13 helpfully suggests.
2. Also, the size property is a member of the FileSystemObject and not WIA.

I would add to these points that I ran into errors running your code - namely the declarations of File and Folder. I suspect that VBA may have been confusing them for the Shell data types, so I made it explicitly clear in my proposed rewrite below. I also moved the declarations outside of the loop. It can slow things down unnecessarily. Finally, you need to add the JPG extension to the end of the new filename, otherwise the new extension will be these additional details you're adding to the filename, and I suspect that's not what you intended. Accordingly the code below: (a) removes the .jpg extension from the filename, and moves it the end.

VBA Code:
Sub RenameImages()
    Dim NewName As String
    Dim Image As Object
    Dim FSO As Scripting.FileSystemObject
    Dim FSOFolder As Scripting.Folder
    Dim FSOFile As Scripting.File
 
    Set FSO = New FileSystemObject
    Set FSOFolder = FSO.GetFolder("D:\temp") ' Replace with the path to your folder
    Set Image = CreateObject("WIA.ImageFile")
  
    For Each FSOFile In FSOFolder.Files
        Debug.Print FSOFile.Name
        If FSOFile.Type Like "*JPG*" Then
       
            Image.LoadFile FSOFile.Path
         
            ' Compute the new file name
            NewName = Replace(FSOFile.Name, ".jpg", "") & " - " & FSOFile.Size & " - " & Image.Width & "x" & Image.Height & ".jpg"
         
            ' Rename the file
            FSOFile.Name = NewName
            Debug.Print NewName
        End If
    Next
    
    Set FSO = Nothing
    Set FSOFolder = Nothing
    Set FSOFile = Nothing
    Set Image = Nothing
End Sub

Let us know how that goes.
 
Last edited:
Upvote 0
Solution
Thank you very much! Yes the problem was that it was not JPG Image type, but JPG file!
 
Upvote 0
Glad it's working. Thank you for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top