Macro to rename .jpg and .png files with similar names

selva2k25

New Member
Joined
Jul 11, 2011
Messages
1
Hi Team,

I need to write a Macro, which will find all .jpg and .png files in a folder with same names and rname them seq as 1.jpg, 1.png, 2.jpg, 2.png and so on...................

Please note that even after renaming both files will be under the same names only...............But instead of any other name it has to be from 1, 2, 3, and so on..................

For example assume we have Loc.jpg and Loc.png.............After running the Macro, it has to be renamed as 1.jpg, 1.png..................Please advice................

I already found have a sample code...........But unfortunatly it does not work for me............Please find the code below and help me on the same........................

Code:
Option Explicit
Sub SerializeFiles()
  Dim Counter As Long
  Dim Dict As Object
  Dim Ext As String
  Dim FileName As Variant
  Dim Path As Variant
  Dim oShell As Object
  Dim oFolder As Variant
  Dim oFolderItem As Variant
 
    Path = "C:\Users\SM26018\Pictures\"
    ChDir Path
 
    Set Dict = CreateObject("Scripting.Dictionary")
    Dict.CompareMode = vbTextCompare
 
    Set oShell = CreateObject("Shell.Application")
 
      Set oFolder = oShell.Namespace(Path)
 
      For Each oFolderItem In oFolder.Items
        With oFolderItem
          Ext = LCase(Right(.Name, 3))
 
          If .Type <> "File Folder" And (Ext = "jpg" Or Ext = "png") Then
             FileName = Left(.Name, InStr(1, .Name, ".") - 1)
               If Not Dict.Exists(FileName) Then
                  Dict.Add FileName, 0
               Else
                  Counter = Counter + 1
                  Dict(FileName) = Counter
               End If
          End If
        End With
      Next oFolderItem
 
      For Each FileName In Dict.Keys
       'Change only matched file names
        If Dict(FileName) <> 0 Then
           Name FileName & ".jpg" As Dict(FileName) & ".jpg"
           Name FileName & ".png" As Dict(FileName) & ".png"
        End If
      Next FileName
 
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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