Batch renaming photos according to fields

M

Mike B

I have a database for a store that contains all my inventory information.
These items also have a picture assigned to them. Right now the pictures are
named according to their inventory number in the "Product Number" field. What
I need to do is rename them according to the, for example, "UPC Code" field.

I know that there is a way to do this because someone showed me but it was a
while ago and I cannot remember how they did it. The end result was they
created the code in Access (I think they used the query mode) then saved it
in a text file which they renamed as a .bat file so when you open it, the
command line opens and begins renaming the files. If any one could help, I
would greatly appreciate it. Thank you.
 
P

Philip Herlihy

Mike said:
I have a database for a store that contains all my inventory information.
These items also have a picture assigned to them. Right now the pictures are
named according to their inventory number in the "Product Number" field. What
I need to do is rename them according to the, for example, "UPC Code" field.

I know that there is a way to do this because someone showed me but it was a
while ago and I cannot remember how they did it. The end result was they
created the code in Access (I think they used the query mode) then saved it
in a text file which they renamed as a .bat file so when you open it, the
command line opens and begins renaming the files. If any one could help, I
would greatly appreciate it. Thank you.

I'm assuming it's easy enough to create a query which selects
Product-Number and UPC-Code - right?

In the query builder (or SQL if you prefer) modify the term:
[Product-Number]
....to become:
OLD: "REN "& [Product-Number] & ".jpg "

Then, when you run that, you should see in the output a column headed
"OLD" with lines like this:

REN 123456.jpg
.... followed by the relevant UPC Code.

Then do the same for the second term, so:
[UPC-Code]
... becomes:
NEW:[UPC-Code] & ".jpg"

When you run it now, you should see two columns again, but now the
second one is headed "NEW" and each line should contain something like:
REN 123456.jpg UPC-abcdef.jpg

Publish it to Excel, and save it as a tab-separated file, and then
change the extension to .cmd.

Vital-step: make a copy of the whole folder first!

Then run the script you just created (edit out the header line first).

Caveat, it's late in the evening and this is an untested
"thought-experiment" so watch out!

Phil, London
 
M

Mike B

Sorry for the delay. My first post was kind of vague on some details. I'm not
all that familiar with Access so let me start over. The database that I am
using has a table called ITEMFILE. The pictures are named according to the
field "GROUP" and I need to rename them to match the corresponding field "SKU"

The pictures are stored on another drive (e:) and the database file is on
c: (I can move both to the same location if need be). Also, would there be an
easier way to do this? Excel for example? I don't have to use Access, it is
just how I saw it done. Thanks for your patience!

Mike

Philip Herlihy said:
Mike said:
I have a database for a store that contains all my inventory information.
These items also have a picture assigned to them. Right now the pictures are
named according to their inventory number in the "Product Number" field. What
I need to do is rename them according to the, for example, "UPC Code" field.

I know that there is a way to do this because someone showed me but it was a
while ago and I cannot remember how they did it. The end result was they
created the code in Access (I think they used the query mode) then saved it
in a text file which they renamed as a .bat file so when you open it, the
command line opens and begins renaming the files. If any one could help, I
would greatly appreciate it. Thank you.

I'm assuming it's easy enough to create a query which selects
Product-Number and UPC-Code - right?

In the query builder (or SQL if you prefer) modify the term:
[Product-Number]
....to become:
OLD: "REN "& [Product-Number] & ".jpg "

Then, when you run that, you should see in the output a column headed
"OLD" with lines like this:

REN 123456.jpg
.... followed by the relevant UPC Code.

Then do the same for the second term, so:
[UPC-Code]
... becomes:
NEW:[UPC-Code] & ".jpg"

When you run it now, you should see two columns again, but now the
second one is headed "NEW" and each line should contain something like:
REN 123456.jpg UPC-abcdef.jpg

Publish it to Excel, and save it as a tab-separated file, and then
change the extension to .cmd.

Vital-step: make a copy of the whole folder first!

Then run the script you just created (edit out the header line first).

Caveat, it's late in the evening and this is an untested
"thought-experiment" so watch out!

Phil, London
.
 
P

Philip Herlihy

Ok, nobody's born knowing this stuff, and it's ok to ask for help as long as
you're prepared to work at it a little.

There are a zillion ways of doing this, but you have the existing names in
Access so that's a good starting point. Essentially I've suggested you
extract a list of file names from Access, modifying the Query that does this
so that it produces a script (involving the REN command which renames files)
which you can run to make the changes. I've assumed from what you've said
below that the names are unique in both fields - they'd have to be for this
method to work. Make sure we know enough about the values which are in
these fields to be giving you appropriate advice.

"Product Number" is now "Group", and "UPC-Code" is now "SKU" - is that what
you're saying?

Do you know about queries, and what I mean by the query-builder? Can you
write a query which selects Group and SKU? If so, create the query then
look at the query in "SQL View" (see Help to find out how). Copy the SQL
and post it here.

If you're not ready for that, you'll need to learn about queries. Here's
one place to start:
http://office.microsoft.com/training/training.aspx?AssetID=RC010776611033

Phil

Mike B said:
Sorry for the delay. My first post was kind of vague on some details. I'm
not
all that familiar with Access so let me start over. The database that I am
using has a table called ITEMFILE. The pictures are named according to the
field "GROUP" and I need to rename them to match the corresponding field
"SKU"

The pictures are stored on another drive (e:) and the database file is on
c: (I can move both to the same location if need be). Also, would there be
an
easier way to do this? Excel for example? I don't have to use Access, it
is
just how I saw it done. Thanks for your patience!

Mike

Philip Herlihy said:
Mike said:
I have a database for a store that contains all my inventory
information.
These items also have a picture assigned to them. Right now the
pictures are
named according to their inventory number in the "Product Number"
field. What
I need to do is rename them according to the, for example, "UPC Code"
field.

I know that there is a way to do this because someone showed me but it
was a
while ago and I cannot remember how they did it. The end result was
they
created the code in Access (I think they used the query mode) then
saved it
in a text file which they renamed as a .bat file so when you open it,
the
command line opens and begins renaming the files. If any one could
help, I
would greatly appreciate it. Thank you.

I'm assuming it's easy enough to create a query which selects
Product-Number and UPC-Code - right?

In the query builder (or SQL if you prefer) modify the term:
[Product-Number]
....to become:
OLD: "REN "& [Product-Number] & ".jpg "

Then, when you run that, you should see in the output a column headed
"OLD" with lines like this:

REN 123456.jpg
.... followed by the relevant UPC Code.

Then do the same for the second term, so:
[UPC-Code]
... becomes:
NEW:[UPC-Code] & ".jpg"

When you run it now, you should see two columns again, but now the
second one is headed "NEW" and each line should contain something like:
REN 123456.jpg UPC-abcdef.jpg

Publish it to Excel, and save it as a tab-separated file, and then
change the extension to .cmd.

Vital-step: make a copy of the whole folder first!

Then run the script you just created (edit out the header line first).

Caveat, it's late in the evening and this is an untested
"thought-experiment" so watch out!

Phil, London
.
 
K

KenSheridan via AccessMonster.com

Mike:

You should be able to do this easily enough entirely from within Access. I'd
suggest doing it in two stages, firstly copying the files under the new names,
then deleting the originals after you've checked that they've copied
successfully. You then have an immediate escape route.

I'm assuming in the code below that the Group and SKU columns in your
ItemFile table contain just the file names without the folder path or the
file extension.

Firstly add the following module to the database. This contains code to call
the Windows API CopyFile function:

''''module starts''''
Option Compare Database
Option Explicit

Declare Function CopyFile& Lib "kernel32" Alias "CopyFileA" (ByVal _
lpExistingFilename As String, ByVal lbNewFileName As String, ByVal _
bFailIfExists As Long)


Public Sub MakeFileCopy(strExistingFile As String, _
strNewFile As String, _
blnDoNotOverWrite As Boolean, _
Optional blnShowMessage As Boolean = False)


Dim strMessage As String

strExistingFile = strExistingFile
strNewFile = strNewFile

If CopyFile(strExistingFile, strNewFile, blnDoNotOverWrite) = 1 Then
strMessage = "File successfully copied."
Else
strMessage = "File copy failed."
End If

If blnShowMessage Then
MsgBox strMessage, vbInformation, "Copy File"
End If

End Sub
''''module ends''''

To copy the files add the following procedure:

Public Sub RenameFiles(strFolder As String, strExt As String)

Dim rst As ADODB.Recordset
Dim strSQL As String
Dim strFile As String
Dim strNewFile As String

strSQL = _
"SELECT Group, SKU " & _
"FROM ItemFile"

Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.Open _
Source:=strSQL, _
CursorType:=adOpenForwardOnly

With rst
Do While Not .EOF
' get current file name
strFile = strFolder & "\" & _
.Fields("Group") & "." & strExt
' get new file name
strNewFile = strFolder & "\" & _
.Fields("SKU") & "." & strExt
' copy file under new name
MakeFileCopy strFile, strNewFile, False

.MoveNext
Loop
End With

End Sub


You then just need to call the procedure, passing in the path to the folder
and the extension of the files, e.g.

RenameFiles "E:\Images\ItemImages", "jpg"

That should make a copy of each file in the same folder, taking the file name
from the SKU column in each case. Once you are happy that this has been done
successfully, you can delete the original files with another procedure:

Public Sub KillFiles(strFolder As String, strExt As String)

Dim rst As ADODB.Recordset
Dim strSQL As String
Dim strFile As String

strSQL = _
"SELECT Group " & _
"FROM ItemFile"

Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.Open _
Source:=strSQL, _
CursorType:=adOpenForwardOnly

With rst
Do While Not .EOF
' get original file name
strFile = strFolder & "/" & _
.Fields("Group") & "." & strExt
' delete the original file
Kill strFile

.MoveNext
Loop
End With

End Sub


Call it in the same way:

KillFiles "E:\Images\ItemImages", "jpg"

One thing you must do of course is ensure that you have a current back-up of
the folder in question before doing any of this.

Ken Sheridan
Stafford, England

Mike said:
Sorry for the delay. My first post was kind of vague on some details. I'm not
all that familiar with Access so let me start over. The database that I am
using has a table called ITEMFILE. The pictures are named according to the
field "GROUP" and I need to rename them to match the corresponding field "SKU"

The pictures are stored on another drive (e:) and the database file is on
c: (I can move both to the same location if need be). Also, would there be an
easier way to do this? Excel for example? I don't have to use Access, it is
just how I saw it done. Thanks for your patience!

Mike
[quoted text clipped - 43 lines]
Phil, London
.
 

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