Copy and rename photos from camera to directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SgtTurbo
    New Member
    • Nov 2013
    • 18

    Copy and rename photos from camera to directory

    I want cmdButton_Click () event to move all photos found on the camera (of varying Drive Letter) to a specified directory with a specified file name followed by an increment serial number.

    The drive letter may vary depending on drive letters already mapped and assigned to the user. I have solved this with the For/Next to search if the directory exists with the drive letters A-Z.

    Code:
    Private Sub cmdTest_Click()
        Dim FSO As Object
        Dim pathTo As String
        Dim FileExtension As String
        Dim intDrive As Integer
        Dim strCamFolder As String
        Dim blnStop As Boolean
        Dim intPhoto As Integer
        Dim pathFrom As String
        Dim FileNames As String
                   
            Set FSO = CreateObject("Scripting.FileSystemObject")
            pathTo = "C:\Users\UserName\Desktop\TEST\"
            FileExtension = "*.jpg"
            intDrive = 65 'starts at Drive Letter "A"
            strCamFolder = ":\DCIM\100NIKON\"
            blnStop = False
            intPhoto = 0
            
    For i = 1 To 26 'A-Z
        If FolderExists(Chr(intDrive) & strCamFolder) = False Then
            intDrive = intDrive + 1
        Else
            pathFrom = Chr(intDrive) & strCamFolder
            
            Do
                FileNames = dir(pathFrom & FileExtension)
                If Len(FileNames) = 0 Then
                    blnStop = True
                Else
                    intPhoto = intPhoto + 1
                    FileCopy pathFrom & FileNames, pathTo & "FRa120313a" & intPhoto & ".jpg"
    '                FSO.MoveFile Source:=pathFrom & FileExtension, Destination:=pathTo
                End If
            Loop Until blnStop = True
                    
            intDrive = intDrive + 1
        End If
    Next i
    
    End Sub
    I seem to be having trouble figuring out what to write between lines 31-33.

    When I use the FSO.MoveFile on Line 33, it will move all photos from the camera to the drive specified without changing the file names (except their parent path).

    But when I use the FileCopy on Line 32, it will only copy one photo to the specified directory and rename it.

    Ultimately I'd like to just copy the photos rather than move them to avoid any loss of photos during unforeseen failures, AND rename them with the incremental serial numbering suffix. I'd then like to delete each photo from that camera after verifying the copy/transfer/rename was successful. And lastly on my wish list, I'd like to display a MsgBox intNumberPhotos & " transferred."

    Thank you for your help.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    While I take a few moments to digest your code, you might find the answer in the following tutorial: Using The FileSystemObjec t With VB and VBA

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      page two of the linked tutorial, use the move/copy property of the folder to either move or copy the files from you camara to the correct location. Then use the files-colection and rename the files within that folder.
      I thought that could be done; however, I don't use the FSO very often.

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        You can perform the Move & Rename in a single Operation. The following Code will Move the File Test.mdb from the C:\Test\ Folder to the C:\Stuff\ Folder renaming it in the process to the Value of intCtr & .mdb.
        Code:
        Dim FSO As Object
        Dim strSource As String
        Dim strDest As String
        Dim intCtr As Integer
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
        intCtr = intCtr + 1
        
        strSource = "C:\Test\Test.mdb"
        strDest = "C:\Stuff\"
        
        FSO.MoveFile Source:=strSource, Destination:=strDest & CStr(intCtr) & ".mdb"
        Upon completion:
        Code:
        C:\Test\Test.mdb ==> C:\Stuff\1.mdb

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          ADezii: You can perform the Move & Rename in a single Operation.
          SgtTurbo: Ultimately I'd like to just copy the photos rather than move them to avoid any loss of photos during unforeseen failures, AND rename them with the incremental serial numbering suffix
          Which is why I thought the files collection:
          Code:
          For Each zFile In zfiles
          Next
          Could use the copy, with the rename in the loop, then in a seperate loop start some sort of validation
          I really don't have time to write and test the code today.

          Or is this not the right logic? It is past my lunch time and I haven't gotten to burn anything in the lab today (-_-)

          -z
          Last edited by zmbd; Jan 14 '14, 06:40 PM.

          Comment

          • SgtTurbo
            New Member
            • Nov 2013
            • 18

            #6
            Thanks @zmnd, that article helped me a lot. This is part of what I ended up using:
            Code:
               Dim FSO As Object
                Dim Drv As Object
                Dim d As Object
                Dim File As Object
                    Set FSO = CreateObject("Scripting.FileSystemObject")
                    Set Drv = FSO.Drives
                        For Each d In Drv
                            If d.isready And FolderExists(d & "\DCIM\100NIKON\") Then
            The "For Each d in Drv" returned a list of drive letters and eliminated my need to loop through every possible Drive letter. I could then test the few drives that were returned for my specific folder hierarchy path to ensure I'm looking at my camera.

            As for my needing to copy, rename and then delete the files (in that order) I ended up doing something like this:
            Code:
            FileCopy pathFrom & FileNames, pathTo & "FRi120313a" & intPhoto & ".jpg"
            Kill pathFrom & FileNames
            The FileCopy line allowed me to rename the file at the same time as it copied. I then execute the Kill line immediately afterwards in each loop. This is likely not necessary, but we used to have a batch file (I think thats what it's called) that would at times fail, especially if interrupted by anything: like being unplugged, losing power...) and the photos would have already been deleted. So that original Batch file was modified to only copy the photos and leave the originals on the camera requiring the user to delete them manually on the camera themselves after verifying the transfer was successful.

            I think this method will work, but certainly open to any input regarding anything I may be overlooking.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              before you kill the file, you should go ahead and check that it is there and that at least the file size match.

              Comment

              Working...