Copy PDF files based on Date Modified (1 Viewer)

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi all,

I need macro to copy PDF files from server to local Drive based on and Date Modified and File Name. However I already have some Mac in sheet to copy files based on file name, suggest me macro to add Date modified also.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Show the macro u have so far.
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
PFA having macro am using currently.
 

Attachments

  • New Text Document.txt
    2.2 KB · Views: 108

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Ive seen the code. Do you want to compare the timestamp if the file already exist in destination before copying or what?
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
No I just want to add one more search criteria in addition to file name before copying files.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
So define what is your criteria.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
You can use FileDateTime() function to retrieve the date+time stamp of a file.
After getting the stamp Use DateValue() to return just the date.
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
I need macro to copy files based on date range defined the date range is nothing but date modified.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Code:
Sub CopyFiles_Containing()
Dim fso As Object
Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
Dim c As Range, d As Range, rPatterns As Range, jPatterns As Range
Dim bBad As Boolean
Dim File_Name As String
Dim subfolders As Object
dim dte as date
  
   
    If ActiveSheet.Range("A9") = "" Then
           MsgBox "No document to Search ", , "File Check"
        Exit Sub
    Else
        
    ' select Source folder prompt
       ' MsgBox
    If MsgBox("Select Source folder", vbOKCancel, "Source folder") <> vbOK Then
            Exit Sub
    End If
    
       
    With Application.FileDialog(msoFileDialogFolderPicker)
        
        .InitialFileName = "File Path"
        .AllowMultiSelect = False
        
        If .Show = -1 Then ' if OK is pressed
            sSrcFolder = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
        
    End With
    
    
    ' select target folder prompt
    
    If MsgBox("Select destination folder", vbOKCancel, "Target Folder") <> vbOK Then
    Exit Sub
    End If
     
     
    With Application.FileDialog(msoFileDialogFolderPicker)
    
        .InitialFileName = "C:\"
        .AllowMultiSelect = False
        
        If .Show = -1 Then ' if OK is pressed
            sTgtFolder = .SelectedItems(1) & "\"
        Else
           Exit Sub
        End If
        
    End With
        
    
    Set rPatterns = ActiveSheet.Range("A9:B200").SpecialCells(xlConstants)
            
        For Each c In rPatterns
          
              sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
          
              If sFilename = "" Then
              
                  c.Interior.ColorIndex = 3
                  bBad = True
              
              Else
              
                  While sFilename <> ""
                  dte = FileDateTime(sSrcFolder & sFilename)
                  dte = DateValue(dte & "")
                  If dte >= ActiveSheet.Range('starting date range here) and _
                  dte <= ActiveSheet.Range('ending date range here)  then
                       FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
                  End If
                  sFilename = Dir()
                  
                  Wend
              
              End If
     
           Next c
           
    
        
   If bBad Then MsgBox "Some files were not found. " & _
        "These were highlighted Red for your reference."
    End If


End Sub
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Thanks a lot arnelgp..!!

Am very sorry my actual code is now i have attached in document,I just want to add one more feature to search (Date Modified) in addition to the file name please let me know the required one.
 

Attachments

  • 1.txt
    1.9 KB · Views: 99

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
See the code i posted. It will copy only those files of specified dates. On your excel sheet add 2 cells where you can specify the two dates (modified date after, modified date before). Now on the code i gave just plug in the correct range of these dates.
Code:
...
...
For Each c In rPatterns
          
              sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
          
              If sFilename = "" Then
              
                  c.Interior.ColorIndex = 3
                  bBad = True
              
              Else
              
                  While sFilename <> ""
                  dte = FileDateTime(sSrcFolder & sFilename)
                  dte = DateValue(dte & "")
                  ' example A2 contains 4/1/2018 and
                  ' A3 contains 4/22/2018, meaning you
                  ' only want to copy modified between these dates.
                  If dte >= ActiveSheet.Range("A2").Value and _
                  dte <= ActiveSheet.Range("A3").Value  then
                       FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
                  End If
                  sFilename = Dir()
                  
                  Wend
              
              End If
     
           Next c
...
...
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi arnelgp,

Its not working its throwing error msg as per comment we have defined.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Upload you xl file and ill try to fix it.
 

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi arnelgp,


PFA and I need Date Modified button adjacent to reset which prompts for Date Range I mean If I give 2016-2017 in Search it has to copy files within this range i don't want the files to be copied since beginning just because the file names are matching. Hope this resolves problem.

Thanks for your Support..!!!
 

Attachments

  • Test File.zip
    29.5 KB · Views: 90

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Here please tey.
 

Attachments

  • Test File.zip
    46.4 KB · Views: 91

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi,

It working fine with first code but showing some error while running second code after pressing Server 2 button.Error lines are sFilename = Dir() and lngYear = Year(FileDateTime(sSrcFolder & sFilename)) Kindly let me know what should be done.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Please see the changes in the modules.
 

Attachments

  • Test File.zip
    41.6 KB · Views: 102

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi arnelgp,

Thanks a lot for troubleshooting, whatever done till now is working fine what small change am looking is that whenever we click on Reset even date also should be reset, which is not taking place right now, apart from that after pressing Server1 it was not prompting for Source Folder which it is right now. I just look forward for resolving these with your immense support.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:45
Joined
May 7, 2009
Messages
19,246
Here again is the file
 

Attachments

  • Test File.zip
    41.7 KB · Views: 121

Venkat1067

Registered User.
Local time
Today, 11:15
Joined
Apr 21, 2018
Messages
15
Hi arnelgp,

Its working absolutely fine, but one issue is that its not highlighting in red colour for files which are not found and I don't want pop up for Server 1 to select the source folder,it has to copy files based on the path defined,pop up has to be only for Server2 looking forward for your help in resolving issues.
 

Users who are viewing this thread

Top Bottom