# 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 (Dec 23, 2022)

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!


```
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
```


----------



## rollis13 (Dec 24, 2022)

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:

```
If file.Type Like "*JPG*" Then
```
and also:

```
newName = file.Name & " - " & file.Size & " - " & image.Width & "x" & image.Height
```


----------



## Dan_W (Dec 24, 2022)

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.


```
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.


----------



## Jamualson (Dec 24, 2022)

Thank you very much! Yes the problem was that it was not JPG Image type, but JPG file!


----------



## Dan_W (Dec 24, 2022)

Glad it's working. Thank you for the feedback.


----------

