rename all files in a folder

D

DIBS

This routine renames the file "old.jpg" with a new 8 digit name consisting
of today's day, hour, minute and second.





How can I modify this so it loops through all of the files in that directory
and renames them?



Sub RenamePIX()

Dim x As String, y, z, OldName, NewName

x = "C:\Documents and settings\user\my documents\PIX\"

y = Format(Date, "dd")

z = Format(Time, "hhmmss")

OldName = x & "OLD.jpg": NewName = x & y & z & ".jpg"

Name OldName As NewName

End Sub
 
J

JulieD

Hi DIBS

not sure if i'm understanding correctly what you're after as from my reading
of your code you have multiple files called OLD.JPG in a single folder which
(AFAIK) is impossible and running a macro on multiple files will take less
than a second for each file, which means that you could end up with duplicate
new file names.

Therefore, the code i've written (well modified from some i wrote earlier)
assumes that you have multiple jpg files with different names and also
appends an index number to the new names (the other ways to do this is to
only append an index number if a duplicate name is produced, via error
handling ... or to add code that waits a second or two between each
iteration).

Sub updatefilenames()
Dim oldname As String
Dim newname As String
Dim fname As String
Dim pname As String
Dim y
Dim z
fname = "*.jpg" 'filename
pname = "C:\Documents and settings\user\my documents\PIX\" 'folder to use
Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = pname
.SearchSubFolders = False
.Filename = fname 'check to see if any files match the fname
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
y = Format(Date, "dd")
z = Format(Time, "hhmmss")
oldname = .FoundFiles(i)
newname = pname & y & z & i & ".jpg"
Name oldname As newname
Next
End If
End With
Application.ScreenUpdating = True
End Sub

Hope this helps.
--
Cheers
JulieD


julied_ng at hctsReMoVeThIs dot net dot au
 
J

JulieD

you're welcome and thanks for letting me know.
--
Cheers
JulieD


julied_ng at hctsReMoVeThIs dot net dot au
 

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