Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual Basic
Article

Copy Files from Source to Target location based on the file creation date

Rate me:
Please Sign up or sign in to vote.
1.91/5 (14 votes)
21 Jul 2008CPOL2 min read 38.1K   586   6   4
Copying files from source locaion to target location based on the file creation date

Introduction

This is all about copying files one by one from source location to target location based on the file creation date. This program will copy files created after the cut off date.

Background

This is one of the requirements I got from client. I couldn’t find any good article related to this topic. I decided to write about it so that it might be helpful to others.

Using the code

In this application I am using FileSystemObject object to access the file system on the server. First of all we need to get the folder object. We can use the method GetFolder to get folder object for the specified source path. Once we got the folder object we can get the files one by one. We can use the following statement for getting the folder object

VBScript
Set lstFileDet = objFSO.GetFolder(strSourcePath)

In the above statement objFSO is the FileSystemObject object. strSourcePath contains the path to a source folder (For example "C:\Jans\Source\" ). lstFileDet contains folder object for the specified path.

Next task is to check the file creation date for each of the files available in the source location. We can use the following statement for that

VBScript
i = 0
For Each fileDet In lstFileDet.Files

    If fileDet.DateCreated > dteFileDate Then
            
       lstFiles(i) = fileDet.Name
       i = i + 1
     End If
Next 

In the For loop we are accessing files one by one. Inside the loop we are checking the file creation date with the cut off date (dteFileDate contains cut off date). If the file has created after the cut off date, store the file name to an array.

Now we are having an array with all selected files from the source path. Next task is to copy all files available in the array to the target location. For copying files from one location to another we can use the method CopyFile. We can use the following statement for that

VBScript
objFSO.CopyFile strSourcePath & lstFile(i), strTargetPath & lstFile(i), True

In the above statement objFSO is the FileSystemObject object. strSourcePath contains the path to a source folder (For example "C:\Jans\Source\" ). strTargetPath contains the path to a target folder (For example "C:\Jans\Target\" ). lstFile(i) contains file name where lstFile is an array having list of file names. We can use a for loop to get the file names one by one from the array lstFile . True in the last allows existing files to be overwritten in the target location.

In the application we need to check whether the source folder and target folder exits, we can use FolderExists method for it. This method returns a Boolean value which indicates whether a specified folder exists. It returns True if the folder exists and False if not. We can use the following statement for checking whether source folder exists

VBScript
objFSO.FolderExists(strSourcePath) 

In the above statement objFSO is the FileSystemObject object. strSourcePath contains the path to a source folder (For example "C:\Jans\Source\" ).

Please find the attached VB.6 code for more details

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Capgemini India
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMissing source code Pin
MKauffman22-Jul-08 6:45
MKauffman22-Jul-08 6:45 
GeneralRe: Missing source code Pin
Jans Mary Suncy22-Jul-08 20:10
Jans Mary Suncy22-Jul-08 20:10 
QuestionRe: Missing source code Pin
Brahma reddy badela14-Jul-09 6:06
Brahma reddy badela14-Jul-09 6:06 
GeneralRe: Missing source code Pin
Jim Kolb22-Sep-12 18:27
Jim Kolb22-Sep-12 18:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.