Rename jpg file using vba

B

BobM

I have a directory with .jpg files in it. I need to get the name of
each file and then remove some characters from the file name and copy
it as a new filename. I could also rename the original file. Either one
would work.

Any ideas?
 
D

Dirk Goldgar

BobM said:
I have a directory with .jpg files in it. I need to get the name of
each file and then remove some characters from the file name and copy
it as a new filename. I could also rename the original file. Either
one would work.

Any ideas?

You can use the Dir() function to get the names of all the .jpg files in
the directory, one at a time, and then use either the FileCopy
statement to copy a file, or the Name statement to rename a file,
whichever you prefer.

To loop through all the .jpg files in directory "C:\Foo":

Dim strFolder As String
Dim strFileName As String

strFolder = C":\Foo\"

' Get first matching file.
strFile = Dir(strFolder & "*.jpg")

Do Until Len(strFile) = 0

' ... do something here with strFile ...

' Get next matching file.
strFile = Dir()

Loop
 
B

Bob M

I tried your suggestion and it worked. One of my colleagues suggested
using an array for the names so I added that. Seems to take less time.
But without your suggestion, I would still be trying to figure this one
out. thanks
 

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