• Access 2010: Compare and copy files to new directory

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Access 2010: Compare and copy files to new directory

    Author
    Topic
    #503395

    Good Morning,I have a list of file names and I would like assistance with VBA code to compare that list to a specific folder if there is a match, copy and paste to a destination folder, is this possible? If so, can someone help me with a jump start. I’m thinking I will need to use File Scripting.Thanks so much in advance.

    Viewing 1 reply thread
    Author
    Replies
    • #1540116

      Roberta,

      Are we to assume the list of file names are stored in am Access table?

      If so, What is the table name and field name of the file names. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1540136

        You are correct, the list of file names is in an Access table, tbl_FileNameList_v18 and field name is v18_FileName.

    • #1540157

      Roberta,

      This should do the trick:

      Code:
      Sub CopyFilesInList()
      
         Dim rs       As DAO.Recordset
         Dim zSrcDir  As String
         Dim zDestDir As String
         Dim iCopyCnt As Integer
         
         zSrcDir = "G:BEKDocsScripts"   'Where are the existing files located
         zDestDir = "G:Test"             'Where do you want the files to go!
         
         Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_FileNameList_v18")
         iCopyCnt = 0
         
         'Check to see if the recordset actually contains rows
         If Not (rs.EOF And rs.BOF) Then
          rs.MoveFirst 'Unnecessary in this case, but still a good habit
          Do Until rs.EOF = True
              
            On Error GoTo NextFile
            FileCopy zSrcDir & "" & rs!v18_FileName, zDestDir & "" & rs!v18_FileName
            iCopyCnt = iCopyCnt + 1
      
      NextFile:
            rs.MoveNext   'Move to the next record.
          
          Loop
         Else
          MsgBox "There are no records in the recordset."
         End If
      
         MsgBox "There were " & iCopyCnt & " files copied."
      
         rs.Close 'Close the recordset
         Set rs = Nothing 'Clean up
         
      End Sub    '*** CopyFilesInList ***
      

      Adjust the Source & Destination paths.

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 1 reply thread
    Reply To: Access 2010: Compare and copy files to new directory

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: