Results 1 to 6 of 6
  1. #1
    LonghronJ is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    150

    Copying pdf files from a folder to another

    Hi,

    I'm having a hard time looping through all files in a folder. I get invalid procedure message when it reaches the line "strCurrentFileName = Dir". If I comment that out, it gets stuck on the first file. FileExists() is a function that checks if a file already exists in a given folder.

    Code:
       
                If Len(Dir(strCurrentPdfFolder)) <> 0 Then
                        Set fso = CreateObject("scripting.filesystemobject")
                        strCurrentFileName = Dir(strCurrentPdfFolder & "*.pdf")
                        Do While Len(strCurrentFileName) > 0
                        
                            If Right(strCurrentFileName, 3) = "pdf" Then
    
    
                                strCurrentFolderAndFile = strCurrentPdfFolder & strCurrentFileName
                                strFutureFolderAndFile = strFuturePdfFolder & strCurrentFileName
                                
                                If FileExists(strFutureFolderAndFile) = False Then 
                                    Call fso.CopyFile(strCurrentFolderAndFile, strFutureFolderAndFile, 0)
                                End If
                                
                                strCurrentFileName = Dir
                               
                            End If
                        Loop


  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    You dont need to use Dir(), this can all be done with FSO.

    Code:
        Dim fso As New FileSystemObject
        Dim fil As File
        Dim fol As Folder
        
        Set fol = fso.GetFolder(FolPath)
    
    
        For Each fil In fol.Files
    
    
            If fso.GetExtensionName(fil.Path) = "Pdf" Then
    
    
                Debug.Print fil.Name, fil.Path
    
    
            End If
    
    
        Next
    You can test if the destination folder contains the pdf with fso.FileExists(fil.path)

    You can move or copy the file with fso also.

    Google:
    fso.CopyFile
    fso.MoveFile
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    usage: CopyPdfFilesInSrcDir2TargDir [source Folder], [Target Folder]


    Code:
    Public Sub CopyPdfFilesInSrcDir2TargDir(ByVal pvDir, ByVal pvTargDir)
    Dim fso, oFolder, oFile, vFile
    Dim sTxt As String, sFile As String
    Dim acc As Access.Application
    Dim control As Office.CommandBarControl
    Dim bRun As Boolean
    Dim i As Integer
    On Error GoTo errGetFiles
      'fix targ dir name
    If Right(pvTargDir, 1) <> "\" Then pvTargDir = pvTargDir & "\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(pvDir)
    Set acc = New Access.Application
    For Each oFile In oFolder.Files
         If InStr(oFile.Name, ".pdf") > 0 Then 'only pdf files
             vTargFile = pvTargDir & oFile.Name
             Copy1File oFile, vTargFile
        End If
    Next
    Endit:
    Set oFile = Nothing
    Set oFolder = Nothing
    Set fso = Nothing
    Set acc = Nothing
    Set control = Nothing
    MsgBox "Done"
    Exit Sub
    errGetFiles:
      MsgBox Err.Description, , Err
      Resume Endit
    End Sub
    
    Public Function Copy1File(ByVal pvSrc, ByVal pvTarg) As Boolean
    Dim fso
    On Error GoTo errMake
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile pvSrc, pvTarg
    Copy1File = True
    Set fso = Nothing
    Exit Function
    errMake:
    'MsgBox Err.Description & vbCrLf & pvSrc, , "Copy1File(): " & Err
    debug.print "err: " & pvSrc
    Set fso = Nothing
    End Function

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Your original code slightly modified to account to what I think were missing "" in the paths:
    Code:
    If Len(Dir(strCurrentPdfFolder)) <> 0 Then
       Set fso = CreateObject("scripting.filesystemobject")
       strCurrentFileName = Dir(strCurrentPdfFolder & "\" & "*.pdf")
       'Do While Len(strCurrentFileName) > 0
            Do While strCurrentFileName<> ""
               'If Right(strCurrentFileName, 3) = "pdf" Then 'you don't need this as you already filter on PDFs
                strCurrentFolderAndFile = strCurrentPdfFolder & "\" & strCurrentFileName
                strFutureFolderAndFile = strFuturePdfFolder & "\" & strCurrentFileName                            
                    If FileExists(strFutureFolderAndFile) = False Then 
                        Call fso.CopyFile(strCurrentFolderAndFile, strFutureFolderAndFile, 0)
                    End If                            
                strCurrentFileName = Dir                           
               'End If
            Loop
    End If
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    LonghronJ is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    150
    Thanks, Moke. From the initial test, it seems to work. I will continue to test.

    Quote Originally Posted by moke123 View Post
    You dont need to use Dir(), this can all be done with FSO.

    Code:
        Dim fso As New FileSystemObject
        Dim fil As File
        Dim fol As Folder
        
        Set fol = fso.GetFolder(FolPath)
    
    
        For Each fil In fol.Files
    
    
            If fso.GetExtensionName(fil.Path) = "Pdf" Then
    
    
                Debug.Print fil.Name, fil.Path
    
    
            End If
    
    
        Next
    You can test if the destination folder contains the pdf with fso.FileExists(fil.path)

    You can move or copy the file with fso also.

    Google:
    fso.CopyFile
    fso.MoveFile

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    This is closer to what your looking for than my previous post

    Code:
        Dim fso As New FileSystemObject
        Dim fil As File
        Dim fol As Folder
    
    
        Set fol = fso.GetFolder(strFolderPath)
    
    
        For Each fil In fol.Files
    
    
            If fso.GetExtensionName(fil.Path) = "Pdf" Then   'test for pdf file
    
    
                If Not fso.FileExists(fso.BuildPath(gDestination, fil.Name)) Then     'test that file is not already in destination folder
    
    
                    fso.CopyFile fil.Path, fso.BuildPath(gDestination, fil.Name), False
    
    
                End If
    
    
            End If
    
    
        Next
    
    
        'ListFiles gDestination, Me.lstDestination  'list the files in destination folder
    
    
        Set fso = Nothing
    Here's a demo
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Copying Files from One Folder to Another
    By LonghronJ in forum Modules
    Replies: 9
    Last Post: 06-05-2018, 10:00 AM
  2. Copying files from outside sources
    By Lou_Reed in forum Access
    Replies: 31
    Last Post: 12-05-2016, 08:06 AM
  3. Replies: 10
    Last Post: 09-09-2015, 03:25 AM
  4. Replies: 1
    Last Post: 05-15-2015, 10:58 AM
  5. Replies: 3
    Last Post: 09-02-2014, 01:06 AM

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