Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Copy files based on file name and date created with VBS

Posted on 2008-06-15
5
1,062 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:Hurel
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:nds_rahulmistry
ID: 21792415
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

0
 
LVL 3

Accepted Solution

by:
nds_rahulmistry earned 250 total points
ID: 21792633
I AM SORRY

I GAVE YOU VBA CODE INSTEAD OF VBS CODE.

FOLLOWING IS THE VBS CODE TO GET THE FUNCTIONALITY


    'DECLARE FOLLOWING VARIABLES
 
    bPath = "C:\Temp\1\"            		'DESTINATION PATH
    aBeginWith = "CM001"            		'FILE BEGINS WITH
    aDate = CDate("01-JAN-2008")    		'FILES CREATED AFTER THIS DATE
 
    'CREATE FILESYSTEM OBJECT
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    'SET THE SOURCE PATH
    Set aPath = fso.GetFolder("C:\Temp")	'SOURCE PATH
 
    'SEARCH THE FILES WITH GIVEN CRITERIA AND COPY THEM TO DESTINATION PATH
    For Each file In aPath.Files
    	If RIGHT(file.Name, 4) = ".CSV" and Left(file.Name, Len(aBeginWith)) = aBeginWith and file.DateCreated > aDate Then
		file.Copy bPath & file.name
	End If
    Next

Open in new window

0
 

Author Comment

by:Hurel
ID: 21792650
thanks for this.

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

Author Comment

by:Hurel
ID: 21792797
I've just tried your VBS code ;-)

Works a treat.

Thanks for your help
0
 

Author Closing Comment

by:Hurel
ID: 31467480
thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question