I have image files that are RAW data and have the extension .NEF Whatever edits I make to these files are stored in what is called a sidecar file with the extension .xmp

Given there are many photos and the photo numbering in the camera can only go up to 9999, there is a possibility that I may get duplicate filenames in the same folder over time.


Given the image files and the sidecar files must have the same base name to remain associated, I'd like to append the date the photo was taken to the base name for both files.

The original date can be found within the .xmp file on a line beginning with a specific string.

I want to

  1. find all image files that have a sidecar file
  2. Get the image date from within the sidecar file
  3. Rename both the image file and sidecar file with the date appended at the end of the base name


I'm pretty sure I'm not importing any namespace except System.IO, so this should all be the .NET way, not any VB6.

Option Explicit On
Option Strict On
Option Infer On
Option Compare Text
Imports System.IO
Module PhotoRename

    Sub Main()
        Const DIR_PATH As String = "C:\Temp\"
        Const IMAGE_EXTENSION As String = ".NEF"
        Const SIDECAR_EXTENSION As String = ".xmp"
    End Sub

    Private Sub GetFilesInDirectory(ByVal path As String, ByVal photoExtension As String, ByVal sidecarExtension As String)
        Const EXTENSION_LENGTH As Integer = 4
        Dim arrayOfFiles() As String = Directory.GetFiles(path, "*" & photoExtension)
        Dim baseName As String
        Dim photoFileName As String
        Dim sidecarFileName As String
        Dim originalDate As String
        For arrayIndex As Integer = 0 To arrayOfFiles.Length - 1
            photoFileName = arrayOfFiles(arrayIndex)
            baseName = photoFileName.Substring(0, photoFileName.Length - EXTENSION_LENGTH)
            sidecarFileName = baseName & sidecarExtension
            If File.Exists(sidecarFileName) Then
                originalDate = GetFileDate(sidecarFileName)
                RenamePhotoFiles(baseName, originalDate, photoExtension, sidecarExtension)
            End If
    End Sub

    Private Function GetFileDate(ByVal sidecarFile As String) As String
        Const LINE_START As String = "   exif:DateTimeOriginal="
        Const DATE_STRING_LENGTH As Integer = 10
        Const CHAR_TO_REMOVE As String = "-"
        Dim lines = File.ReadAllLines(sidecarFile)
        Dim targetData As String = Nothing
        For i As Integer = 0 To lines.Length - 1
            If lines(i).StartsWith(LINE_START) Then
                targetData = lines(i)
                Exit For
            End If
        targetData = targetData.Substring(LINE_START.Length + 1, DATE_STRING_LENGTH)
        GetFileDate = targetData.Replace(CHAR_TO_REMOVE, "")
    End Function

    Private Sub RenamePhotoFiles(ByVal oldName As String, ByVal fileDate As String, ByVal photoExtension As String, ByVal sidecarExtension As String)
        Const FILE_EXTENDER As String = "_"
        Dim newName As String = oldName & FILE_EXTENDER & fileDate
        File.Move(oldName & photoExtension, newName & photoExtension)
        File.Move(oldName & sidecarExtension, newName & sidecarExtension)
    End Sub

End Module

1 Answer 1


Important change

I would suggest you look at the Path class. A lot of your logic is about manipulating filename and this class does it all for you.

Less important change

Depending on how big the file is, doing File.ReadAllLines might take more memory than needed. Reading one line at a time would be better.

GetFilesInDirectory function name isn't true. It also renames the file.

You could use a bit of error checking. For example, what happen if the string "exif:DateTimeOriginal=" can't be found in the file. What if there's no xmp file to the nef file.

  • \$\begingroup\$ Thanks. Since the GetFilesInDirectory calls on RenamePhotoFiles, should I have them both come from Main or should I rename it to something like GetFilesAndRename? \$\endgroup\$ Jun 7, 2017 at 18:51
  • \$\begingroup\$ @Raystafarian that's a good name. The GetFile part is only one line in your function :) \$\endgroup\$
    – the_lotus
    Jun 8, 2017 at 11:28
  • \$\begingroup\$ I'll definitely take a look at the Path Class, thank you. \$\endgroup\$ Jun 9, 2017 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.