Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 21 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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

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

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

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)

Share

About the Author

Jans Mary Suncy
Software Developer Capgemini India
India India
No Biography provided

Comments and Discussions

 
GeneralMissing source code PinmemberMKauffman22-Jul-08 7:45 
GeneralRe: Missing source code Pinmemberjansmary22-Jul-08 21:10 
QuestionRe: Missing source code PinmemberBrahma reddy badela14-Jul-09 7:06 
GeneralRe: Missing source code PinmemberJim Kolb22-Sep-12 19:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 22 Jul 2008
Article Copyright 2008 by Jans Mary Suncy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid