Link to home
Start Free TrialLog in
Avatar of Hurel
Hurel

asked on

Copy files based on file name and date created with VBS

Hi

I'm very new to VBS but slowly getting to grips with it.
I'm trying to copy file from one folder to another based on the file name and the date the file was created.
I need to copy any file in c:\folder_a to c:\folder_b which has the file extention of .cvs, which was created after a certain date,and has a file name which begins  a certain string of characters.

An example of the file name is CMA910001_BA_20080604_101509.CSV
Only .CSV files that begin with CMA910001 created after a certain date need to be copied.

Could somebody please show me an example of this code or point me in the direction of a good web resource that will

thanks
Avatar of nds_rahulmistry
nds_rahulmistry

RUN THE FOLLOWING CODE

CHANGE SOURCE AND TARTET DIRECOTORY PATH IN YOUR CODE.

DO NOT FORGET TO USE REFERENCE, AS DISCRIBED IN CODE

REGARDS,

RAHUL

Private Sub A()
    'You must install a refernce to library:  Microsoft Scripting Runtime
    'IN EXCEL'S VISUAL BASIC EDITOR DO FOLLOWING :
    'CLICK TOOLS - CHOOSE REFERENCE - SELECT : MICROSOFT SCRIPTING RUNTIME - OK
 
    Dim fso As New FileSystemObject 'FILE SYSTEM OBJECT
    Dim aFile As File               'HANDLE FOR FSO FILE OBJECT
    Dim aPath As String
    Dim bPath As String
    Dim aName As String
    Dim aBeginWith As String
    Dim aDate As Date
 
    aPath = "C:\Temp\"              'SOURCE PATH
    bPath = "C:\Temp\1\"            'DESTINATION PATH"
    
    aBeginWith = "CM001"            'FILE BEGINS WITH
    aDate = CDate("01-JAN-2008")    'FILES CREATED AFTER THIS DATE
    
    aName = Dir(aPath)
    Do While Not aName = ""
        If Left(aName, Len(aBeginWith)) = aBeginWith And Right(aName, 4) = ".CSV" Then
            Set aFile = fso.GetFile(aPath & aName)
            If aFile.DateCreated > aDate Then
                FileCopy aPath & aName, bPath & aName
            End If
            Set aFile = Nothing
        End If
        aName = Dir
    Loop
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nds_rahulmistry
nds_rahulmistry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hurel

ASKER

thanks for this.

I'm getting an error
line 6
char 13
error Expected end of statement
code 800a0401
source M$ VBScript compilation error
Avatar of Hurel

ASKER

I've just tried your VBS code ;-)

Works a treat.

Thanks for your help
Avatar of Hurel

ASKER

thanks