ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   rename all files in a folder (https://www.excelbanter.com/excel-programming/349545-rename-all-files-folder.html)

DIBS

rename all files in a folder
 
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





Julied

rename all files in a folder
 
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


"DIBS" wrote:

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






DIBS

rename all files in a folder
 
Dear Julie,

Thank you so very much.
That works beautifully.

rgds,
DIBS


"JulieD" wrote in message
...
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


"DIBS" wrote:

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








Julied

rename all files in a folder
 
you're welcome and thanks for letting me know.
--
Cheers
JulieD


julied_ng at hctsReMoVeThIs dot net dot au


"DIBS" wrote:

Dear Julie,

Thank you so very much.
That works beautifully.

rgds,
DIBS


"JulieD" wrote in message
...
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


"DIBS" wrote:

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










All times are GMT +1. The time now is 10:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com