Copy files to new folder based on modified date.....

Arvind Nolkha

New Member
Joined
Jan 30, 2010
Messages
19
Hi All,

I have a macro which copies the files to another folder based on modified date. Below is the extract of code in Macro :
Code : If InStr(1, objFile.DateLastModified, "9/11/2011") Then

This code doesn't work if more than one date is mentioned.

I need to get this code modified to copy all files which are greater than the mentioned modified date. Can someone please help on this and provide the correct code / new macro for the same.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
A very simple fix which should work, but without seeing more of your code, no guarantees...
Code:
If objFile.DateLastModified > #9/11/2011# Then
Hope that helps,
Cindy
 
Upvote 0
Hi...thanks for the response...but it's not working....have detailed out the entire code for your reference...Thanks

Code:
Sub Copy_Files_To_New_Folder()
     
    Dim objFSO As FileSystemObject, objFolder As Folder, PathExists As Boolean
    Dim objFile As File, strSourceFolder As String, strDestFolder As String
    Dim x, Counter As Integer, Overwrite As String
     
    Application.ScreenUpdating = False 'turn screenupdating off
    Application.EnableEvents = False 'turn events off
     
    strSourceFolder = "C:\DATA\Desktop\10.11.2011\" 'Source path
    strDestFolder = "C:\DATA\" 'destination path, does not have to exist prior to execution
    Set objFSO = New FileSystemObject
    Set objFolder = objFSO.GetFolder(strSourceFolder) 'get the folder
    Counter = 0 'set the counter at zero for counting files copied
          
    For Each objFile In objFolder.Files 'for every file in the folder...
    
         If InStr(1, objFile.DateLastModified, "9/11/2011") Then
         'If InStr(1, objFile.Name, ".txt") Then ' Will copy only Text files
         
        objFile.Copy strDestFolder & "\" & objFile.Name 'use the destination path string, add a / separator and the file name
         
         'objFile.Move strDestFolder & "\" & objFile.Name  'Syntax for MOVING file only, remove the ' to use
         
        Counter = Counter + 1 'increment a count of files copied
         
        End If 'where conditional check, if applicable would be placed.
         
         ' Uncomment the If...End If Conditional as needed
         
    Next objFile 'go to the next file
     
    MsgBox "All " & Counter & " Files from " & vbCrLf & vbCrLf & strSourceFolder & vbNewLine & vbNewLine & _
    " copied/moved to: " & vbCrLf & vbCrLf & strDestFolder, , "Completed Transfer/Copy!"
     'Message to user confirming completion
     
    Set objFile = Nothing: Set objFSO = Nothing: Set objFolder = Nothing 'clear the objects
     
    Exit Sub
End Sub
 
Upvote 0
I replaced the target line in your code with
Code:
         If objFile.DateLastModified > #11/18/2011# Then
with no other changes other than directory names (and on my system, I had to create the target folder before executing the macro), and it worked fine, copying only the files modified on or after yesterday (November 18).
I wonder if it's a local date formatting issue (is 11/9/2011 November 9, 2011 or September 11, 2011?) ...in the VBA code, try swapping the month and day between the two hash marks to "11/9/2011"
Cindy
 
Upvote 0
Try this...

'Date:18-Nov-2011
'Use DateSerial function instead of ##11/18/2011##
If objFile.DateLastModified > DateSerial(2011, 11, 18) Then
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top