Renaming Pictures

M

Mike B

I have a database for my store inventory that includes pictures that are
named according to a column in the database. However, I need to rename all of
the pictures to correspond with a different field (i.e. the pictures are
currently named after the "item" field and I need to rename them to match the
"sku" field) and they are all in the same folder.

There are about two thousand pictures so renaming them one at a time would
not be ideal. I know that there is a way to do this because someone showed me
a while ago but I can not for the life of me remember how they did it. If
anyone can help I would be forever grateful! Thanks a bunch.

Mike B
 
R

Roger Carlson

Create a query (Query1) that lists the sku and item number for your table.
Create and run a small function like this:
'-----------------------
Sub RenameFiles()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim folder As String

Set db = CurrentDb
Set rs = db.OpenRecordset("Query1")
folder = "C:\datafiles\"

Do While Not rs.EOF
Name folder & rs!Item & ".jpg" As folder & rs!sku & ".jpg"
rs.MoveNext
Loop
End Sub
'-----------------------
You'll use the actual names of your query and folder.

This code will crash if any records in your query don't have a corresponding
picture. To get around that, add the following error trap code:

'-----------------------
Sub RenameFiles()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim folder As String

On Error GoTo RenameFiles_Error

Set db = CurrentDb
Set rs = db.OpenRecordset("Query1")
folder = "C:\datafiles\"

Do While Not rs.EOF
Name folder & rs!Item & ".jpg" As folder & rs!sku & ".jpg"
rs.MoveNext
Loop

On Error GoTo 0
Exit Sub

RenameFiles_Error:
If Err.Number = 53 Then
Resume Next
Else
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure RenameFiles of Module Module1"
End If
End Sub
'-----------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top