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