+ Reply to Thread
Results 1 to 4 of 4

rename all files in a folder

  1. #1
    DIBS
    Guest

    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





  2. #2
    JulieD
    Guest

    RE: 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
    >
    >
    >
    >
    >


  3. #3
    DIBS
    Guest

    Re: rename all files in a folder

    Dear Julie,

    Thank you so very much.
    That works beautifully.

    rgds,
    DIBS


    "JulieD" <[email protected]> wrote in message
    news:[email protected]...
    > 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
    >>
    >>
    >>
    >>
    >>




  4. #4
    JulieD
    Guest

    Re: 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" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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
    > >>
    > >>
    > >>
    > >>
    > >>

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1