Thursday, April 3, 2014

Vbscript to copy files from source to dest folder


Have you ever faced a situation where you need to copy files from one location and paste them to destination location repetitively with some specific constrains. Like

I had an requirement where i need to copy 200 files from source and paste them to destination folder, only if there is no file present in dest folder. Here dest folder process the file one by one and load it to database. once all files are processed again i need to copy 200 files and paste it. continues human attention required to check destination file count reach to zero. To overcome this,  here is a simple vbscript code

This vbscript program  will check if there any file on dest folder. if it found no file then it will copy files from source to given destination path. once 200 files are copied then it will break as it goes into  if condition. You can schedule this program to run specified time.

========================================================================


Const destPATH = "D:\dest\Files\"
Const sourcePATH = "D:\source\Files\"
dim fso
dim destfolder,sourcefldr,i
i=0
set fso = CreateObject("Scripting.FileSystemObject")
set destfolder = fso.getFolder(destPATH)
set sourcefolder = fso.getFolder(sourcePATH)
               
If destfolder.files.Count <= 0 then
                                Set sourcefldr=fso.getFolder(sourcePATH)
                                                if sourcefldr.files.count > 0 then           
                                                                for each file in sourcefldr.files              
                                                                                fso.MoveFile file, destfolder + "\"                                                                        
                                                                                i=i+1
                                                                                                if i >=200 then                                                                                       
                                                                                                                exit for
                                                                                                end if
                                                                next
                                                end if      

end if

set fso = nothing
set destfolder = nothing
set sourcefolder = nothing

No comments: