Monday, January 28, 2008

Copy a list of files specified in an Excel Sheet from one location to another location using VBscript

'This function is used to copy a list of files specified in an Excel Sheet from

'Declaration Section
Dim sExcelFile, sSource , sDestination
sExcelFile = "C:\Documents and Settings\BanduvvE\Desktop\fileslist.xls"
sSource = "D:\MyFiles\Generaldocs\"
sDestination = "D:\myfilecopy\"

'Calling a function which copies files specified in the Excel sheet from source to destination
call CopyFiles(sExcelFile, sSource, sDestination)


'Implementation or body of "copyfiles" function
Function CopyFiles(ByVal sExcelPath, ByVal sSourcePath, ByVal sDestinationPath)

'Declaration of Function Variables
Dim objFSO ' Object type for FileSystemObject
Dim objFolder_ ' Object type for Folder Object
Dim objFileColl ' Object type for FilesCollection Object
Dim objExcel ' Object type for Excel application Object
Dim objWorkBook ' Object type for Excel Workbook Object
Dim sFilePath 'Variable which stores the path of the file to be copied
Dim iExcelRow 'Variable which stores the count of rows in the ExcelSheet
Dim iFileCount 'Variable which stores the count of files that are copied to the destination folder
Dim sFileName 'Variable which stores the path of each file accessed thru FilesCollection Object

'Creating instances for objects which are used in this function
Set objExcel = CreateObject("Excel.Application") 'Create instance for the Excel Application
Set objWorkBook = objExcel.Workbooks.Open(sExcelPath) 'Create instance for the Workbook which was opened thru Excel Application
Set objFSO = CreateObject("Scripting.FileSystemObject") 'Create instance for the FileSystemObject
Set objFolder = objFSO.GetFolder(sSourcePath) 'Create instance for Folder object to access the source folder
Set objFileColl = objFolder.Files 'Create instance for FilesCollection Object to access files retrieved by that object

'Initialization of required variables
iExcelRow = 2
iFileCount = 2

'Main part of the function implementation
While objExcel.Cells(iExcelRow, 1).Value <> "" 'This loop is used to read the data from the Excel Sheet row by row
'Excel Sheet has only filenames and we need the whole path for each file
'So concatenate the source folder path with the filename retrieved from the Excel sheet.
sFilePath = sSourcePath & objExcel.Cells(iExcelRow, 2).Value 'sFilePath has the filename with full path
'This loop reads each file from the FilesList returned by the FilesCollection Object
For Each sFileName in objFileColl 'For Each Loop can be used only for accessing individual objects from a collection
If sFileName = sFilePath Then 'Comparing the filename from the ExcelSheet with the filename from the fileslist
'CopyFile function is used to copy a file from current location to another location
'This function has 3 arguments -- filename to be copied, to which location it has to be copied and should overwrite if that file already exists
objFSO.CopyFile sFileName, sDestinationPath, True
iFileCount = iFileCount + 1 'Incrementing the filecount once that file is copied
End If
Next
iExcelRow = iExcelRow + 1 'Incrementing the Excel sheet row count
Wend 'End of the loop
If iExcelRow = iFileCount then
MsgBox "All Files Copied Successfully into the folder -- " & sDestinationPath
End If

'Closing the Excel Application
objExcel.Quit

'End of the function implementation
End Function

No comments: