Rename Images from Folder to match an Excel worksheet

Cruiser69

Board Regular
Joined
Mar 12, 2018
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hi all.

I've searched but can't find quite what I'm looking for.

Is there a way to rename jpg images in a folder to match the numbers on a worksheet

The images in the folder will be named the same as the codes in Column B

I want to rename the codes to match the numbers in Column A

The problem is that some of the codes will be duplicated, as in numbers 4 & 6

The codes will change on a weekly basis.

I can copy the image folder to the desktop so I can keep the original filenames for the next week.


AB
LotCode
1AA12
2BB23
3DE22
4RR23
5BB12E3
6RR23
7FF22


Thanks for looking

Graham
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
try this code:
VBA Code:
Sub copyFiles()
    Const sourceFolder = "...\Desktop\input\" 'Adjust as necessary
    Const fExt = ".jpg"  'Adjust as necessary
    Const destinationFolder = "...\Desktop\output\"  'Adjust as necessary
    Const namesRangeAddress = "B2:B8"  'Adjust as necessary
    
    Dim namesRng As Range, cc As Range
    Set namesRng = ActiveSheet.Range(namesRangeAddress)
    
    Dim fName As String, fPath As String
    Dim fNewPath As String, fNewName As String
    Dim fso As Object, oFile As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'On Error Resume Next
    For Each cc In namesRng
        fName = cc.Value
        fNewName = cc.Offset(, -1).Value
        fPath = sourceFolder & fName & fExt
        fNewPath = destinationFolder & fNewName & fExt
        With fso
            If .fileexists(fPath) Then
                .CopyFile fPath, fNewPath, True
            End If
        End With
    Next cc
    Set fso = Nothing
    Set cc = Nothing
    Set namesRng = Nothing
    MsgBox "Done."
End Sub
Adjust as necessary the folder paths, file extension, names range.
A bit too simple but works if all is setup properly.
I can think of some extras and improvements but this will get you started.
 
Upvote 0
Thanks for this.
I have tried it on a couple of folders. It works well but seems to miss some images
 
Upvote 0
The code does not check all images in the folder. It gets the names from the table, then copies the relevant image to a new folder with a new name.
If some image names are not in the table they will not be processed. Also the code will not know what new name to assign to them.
 
Upvote 0

Forum statistics

Threads
1,222,905
Messages
6,168,948
Members
452,227
Latest member
sam1121

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