Copy files based on query

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy files based on query

Post by HansV »

Sorry, I actually forgot the Next statement.

Code: Select all

Sub GoogleImages()
    Const strSource = "L:\mmpdf\ConsoleFiles\"
    Const strTarget = "C:\Users\dave.willett\Google Drive\ImagePath\"
    Dim fso As Object
    Dim fld As Object
    Dim fil As Object
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim i As Long

    On Error Resume Next
    If Len(Dir("C:\Users\dave.willett\Google Drive\ImagePath", vbDirectory)) > 0 Then

        Set fso = CreateObject("Scripting.FileSystemObject")
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("qryAllInProgressGallery", dbOpenDynaset)
        Do While Not rst.EOF
            If fso.FolderExists(strSource & rst!JobID) Then
                Set fld = fso.GetFolder(strSource & rst!JobID)
                i = 0
                For Each fil In fld.Files
                    If LCase(Right(fil.Name, 4)) = ".jpg" Then
                        fil.Copy strTarget
                        i = i + 1
                        If i = 10 Then Exit For
                    End If
                Next fil
            End If
            rst.MoveNext
        Loop

        rst.Close
        Set rst = Nothing
        Set dbs = Nothing
        Set fso = Nothing
    End If
End Sub
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Copy files based on query

Post by D Willett »

Spot on Hans ( I couldn't see that ).

Many Thanks and Happy New Year.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Copy files based on query

Post by HansV »

Happy 2021 to you too (only 34 hours to go...)
Best wishes,
Hans