Renaming files in a folder using VBA

April 20, 2009JPNo CommentsRate This ArticlenewLinks to this article


    I offloaded close to 400 pictures from my wife's camera, when she decided she didn't want them removed. However, the built-in Windows XP importer had already deleted the photos from the camera's 4 GB MS Pro Duo memory card (my mistake). And of course it had kindly renamed all of the photos as well.

    When I tried to put the photos back on, the camera wouldn't recognize them, because the filename had changed. Changing 400 filenames manually wasn't something I was interested in doing, so I turned to VBA to solve this problem.

    Here's some code that will loop through a folder and rename all the files. It's a hard-coded, quick-n-dirty routine that you'll need to adapt. The filenames all have to be in the following format to be recognized by the camera: DSCnnnnn.JPG. At some point in the future I'll function-ize this to make it more flexible.

Sub ChangeFilename()

Const FILEPATH As String = _
"C:\Documents and Settings\Jimmy Pena\Desktop\camera photos\"

Dim strfile As String
Dim filenum As String

strfile = Dir(FILEPATH)

Do While strfile <> ""
  Debug.Print strfile
  If Right$(strfile, 3) = "jpg" Then
    filenum = Mid$(strfile, Len(strfile) - 6, 3)
    Name FILEPATH & strfile As FILEPATH & "DSC00" & filenum & ".JPG"
  End If

  strfile = Dir

Loop

End Sub

    A Do Loop is employed after grabbing the first filename in the folder with the Dir function. There was a mix of jpg and mpg files from the camera in the folder, so I focused on the jpg files first. Windows renamed the files, but left a three digit number in the filename, which I assigned to the filenum variable. Then I used the "NAME x As y" method to change the filename to a legal one (for the camera, anyway). The loop continues until all jpg files are renamed.

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

4 Response(s) to Renaming files in a folder using VBA ↓

  1. Sumit says:

    Thanks a ton, JP. I was stuck in a similar situation also. God!! you saved me from lots and lots of hassel.

    Sumit.

  2. Mohit says:

    Hi JP,

    I wanted to print all the files in a particular folder which includes .XLs, .Doc & .PDF files. I have used your above code which is able to identify all the files, but can you help me out in finding the way to print them as well without the user intervention.

    Thanks,
    Mohit

  3. Sabbath says:

    Thank you. Your code helped me with a problem I had for I needed to randomly rename my picture files for a slideshow I was creating for my son's football team.

    Sub RandomlyRenamePictureFiles()

        Const sFilePath As String = _
        "C:\Documents and Settings\Sabbath\Desktop\Football\2009\CD\"

        Dim sFile As String
        Dim sFileNumber As String

        sFile = Dir(sFilePath)

        Do While sFile <> ""
            If Right$(sFile, 3) = "jpg" Then
            sFileNumber = Int((9999 - 1000 + 1) * Rnd + 1000)
            Debug.Print sFile & " = " & sFileNumber & ".JPG"
            Name sFilePath & sFile As sFilePath & sFileNumber & ".JPG"
        End If

        sFile = Dir

        Loop

        MsgBox "File renaming completed."

    End Sub

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Learn how to create Excel dashboards.
Become an ExcelUser Affiliate

Site last updated August 24, 2010 @ 5:56 pm