-2

I verified all the directory paths exist so I am completely at a loss here.

For Each i As Project.ImageryCaptureTask.FileProperties In t.FileCollection
    Dim NewFilename As New FileInfo(Path.Combine(Root.FullName, i.LVItem.Text))
    If NewFilename.Exists Then
        ' ...Things get done
    Else
        Try
            i.FI.MoveTo(NewFilename.FullName) ' <- Error thrown here (obviously)
        Catch IOEx As IOException
            MsgBox(IOEx.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source)
        Catch Ex As Exception
            MsgBox(Ex.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source)
        End Try
    End If
Next

Edit

Sorry, I was trying to keep the question short and quick, but I failed to provide the key information about Root.FullName. Here is the snippet to show that.

' P.MainFolder.FullName is the DirectoryInfo instantiated from the FolderBrowserDialog
If Directory.Exists(P.MainFolder.FullName) Then
    For Each t As Project.ImageryCaptureTask In P.TaskCollection
        Dim SubFolder3DMapping As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.Aerial3DMapping.ToString.Insert(6, "_")))
        Dim SubFolderPhotos As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.AerialPhotos.ToString.Insert(6, "_")))
        Dim SubFolderVideos As New DirectoryInfo(Path.Combine(P.MainFolder.FullName, (Val(t.TaskCreationDate) + t.OffsetDate).ToString & "_" & P.ProjectNumber & "_" & P.ProjectName & "_" & Project.ImageryCaptureTask.CapturingMethods.AerialVideos.ToString.Insert(6, "_")))
        If Not SubFolder3DMapping.Exists Then SubFolder3DMapping.Create()
        If Not SubFolderPhotos.Exists Then SubFolderPhotos.Create()
        If Not SubFolderVideos.Exists Then SubFolderVideos.Create()
        Dim Root As DirectoryInfo = Nothing
        Select Case True
            Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.Aerial3DMapping
            Root = SubFolder3DMapping
            Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.AerialPhotos
            Root = SubFolderPhotos
            Case t.CaptureMethod = Project.ImageryCaptureTask.CapturingMethods.AerialVideos
                Root = SubFolderVideos
        End Select
        For Each i As Project.ImageryCaptureTask.FileProperties In t.FileCollection
            Dim NewFilename As New FileInfo(Path.Combine(Root.FullName, i.LVItem.Text))
            If NewFilename.Exists Then
            ' This should not happen, but if it does it gets handled here.
            Else
                Try
                    i.FI.MoveTo(NewFilename.FullName)
                Catch IOEx As IOException
                    MsgBox(IOEx.Message & vbCr & vbCr & "Error renaming to " & NewFilename.FullName, vbOKOnly, IOEx.Source)
                Catch Ex As Exception
                    MsgBox(Ex.Message)
                End Try
            End If
        Next
    Next
End If

Here is a photo showing the results from the last attempt. Of the 33 files, 7 of them failed to be renamed:

enter image description here

The goal here is to rename the image files created during our drone flights. These flights are static, same amount of images at the same GPS locations, so we want to be able to reference any existing flight images so we can extract the filenames. The only dynamic part of the filenames are the first 8 characters (Date: yyyymmDD).

John89
  • 45
  • 1
  • 7
  • 1
    Nobody knows what those collections and objects contain, `Root.FullName` enters the scene from nowhere, so yes, you need to debug this code, write to the output Window what each of those contain and, if you don't find the problem yourself, post the results here. -- IO functions usually don't lie to you. – Jimi Sep 21 '21 at 22:08
  • 'Root.FullName' is just an instance of a 'DirectoryInfo' that already exists. – John89 Sep 22 '21 at 13:07
  • What are the values of `NewFilename.FullName`, `Root.FullName` and `i.LVItem.Text` when the exception is thrown? – LarsTech Sep 22 '21 at 14:48
  • `NewFilename.FullName` is `New FileInfo(Path.Combine(Root.FullName, i.LVItem.Text))`, `Root.Fullname` is 1 of 3 folders created within the directory selected from the `FolderBrowserDialog`, `i.LVItem.Text` is the name extracted from existing files located within the directory selected from the `FolderBrowserDialog`. – John89 Sep 22 '21 at 15:08
  • It's not what I asked. Use the debugger to look at the values of those variables when the exception is thrown. On Stackoverflow, use the @ sign in front of the username to send them a reply in comments. – LarsTech Sep 22 '21 at 15:11
  • @LarsTech I posted the values in the answer. Thank you! – John89 Sep 22 '21 at 16:07

1 Answers1

0

It turns out that the issue is that some of the filenames are too long and could not be renamed.

To get passed the issue with filename lengths being longer than MAX_PATH I used the following threads:

Resolving filename longer than MAX_PATH Exceptions > Forum
Deal with filename longer than MAX_PATH > Forum ... [ specifically the answer by Wolf5 ]

John89
  • 45
  • 1
  • 7