Results 1 to 8 of 8
  1. #1
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662

    Copy Files from One Folder to Another based on File Names in Access Table

    Raw material :

    1) 1st Folder on Desktop ( fldrData ) containing Word Documents ( eg. a.doc, b.doc, c.doc, d.doc ).


    2) Access Table ( tblRequiredDocs ) with single field ( txtWantedDoc - PK ). Current values in this field are b & d ( separate records ).
    3) 2nd Folder on Desktop ( fldrMissingData ), which is empty.

    What needs to be done ?

    The files from fldrData ( b.doc & d.doc - the values in tblRequiredDocs - b & d), need to be copied to fldrMissingData.

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Open recordset, loop through recordset, use txtWantedDoc value to construct file paths.

    Two ways to copy https://www.accessforums.net/access/...opy-45903.html

    1. FileCopy

    2. Windows API
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Hi June,
    Thanks for the pointers.
    Will give it a go & keep you posted ( Might take a bit of time, with my great VBA skills ) .

    Thanks

  4. #4
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Ended up with this, with copy-paste from here & there ( It appears to be working ).

    Code:
    Function CopyDocs()
        
        Dim RequiredDocs As DAO.Database
        Dim rs As DAO.Recordset
        Dim strsql As String
        Dim GetDoc As String
        Dim GetDocFull As String
        Dim SourceA As String
        Dim Dest As String
        
        Set RequiredDocs = CurrentDb
    
    
        SourceA = "C:\myDesktop\fldrData\"
        Dest = "C:\myDesktop\fldrMissingData\"
    
    
        strsql = "SELECT * FROM tblRequiredDocs"
        Set rs = CurrentDb.OpenRecordset(strsql)
    
    
        '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
                'Perform an edit
                rs.Edit
                GetDoc = rs("txtWantedDoc")
                GetDocFull = GetDoc & ".doc"
    
    
                FileCopy SourceA & GetDocFull, Dest & GetDocFull
                
                'Move to the next record. Don't ever forget to do this.
                rs.MoveNext
            Loop
        Else
            MsgBox "There are no records in the recordset."
        End If
        
        MsgBox "Finished looping through records."
    
    
        rs.Close 'Close the recordset
        Set rs = Nothing 'Clean up
    End Function
    Kindly let me know, if you find some pitfalls in it.

    Thanks

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Don't think need rs.Edit.

    Don't need GetDoc variable.

    GetDocFull = rs!txtWantedDoc & ".doc"

    For such a simple sql, I wouldn't bother with variable. Also, just pull the necessary field(s).

    Set rs = CurrentDb.OpenRecordset("SELECT txtWantedDoc FROM tblRequiredDocs;")

    Probably should move the finished looping message box to just under Loop. Don't want both message boxes to open if there are no records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Quote Originally Posted by June7 View Post
    Don't think need rs.Edit.
    Should have seen that.

    Quote Originally Posted by June7 View Post
    Don't need GetDoc variable.
    GetDocFull = rs!txtWantedDoc & ".doc"
    Me & my tedious way of doing things.

    Quote Originally Posted by June7 View Post
    For such a simple sql, I wouldn't bother with variable. Also, just pull the necessary field(s).

    Set rs = CurrentDb.OpenRecordset("SELECT txtWantedDoc FROM tblRequiredDocs;")

    Probably should move the finished looping message box to just under Loop. Don't want both message boxes to open if there are no records.
    What was I doing with a *, when there is only one field in the table ?

    Thanks a lot for the inputs.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    If there is only one field in table, then the wildcard is just fine, either way only one field is pulled. Except for the message box location, what you had didn't hurt anything. I just thought would be a little cleaner.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Thanks for the update.
    Agree totally, the code sure has become cleaner.

    Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to copy a file from one folder to another in vba
    By pkstormy in forum Code Repository
    Replies: 2
    Last Post: 09-20-2012, 05:32 PM
  2. count number of file names within a folder containing
    By dumbledown in forum Programming
    Replies: 2
    Last Post: 04-24-2012, 02:55 AM
  3. Copy files into specific folder
    By Jan22 in forum Access
    Replies: 12
    Last Post: 04-17-2012, 08:27 AM
  4. Copy file to folder
    By tpcervelo in forum Programming
    Replies: 11
    Last Post: 08-31-2010, 10:01 AM
  5. copy data from text file into table in access
    By miziri in forum Programming
    Replies: 3
    Last Post: 08-12-2009, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums