Link to home
Start Free TrialLog in
Avatar of uucutech
uucutech

asked on

Copy folder contents using vbscript

hello,

We have forms that are constantly being updated on our system and periodically need to be pushed to our desktops.  I was wondering if there is a way (using vbscript) to copy the contents of a folder from a network share, to a folder on the local HD.  It would also be nice to have the script check to see if the files already exist and not copy over them if they do.

any suggestions?


Thanks in advance!
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I think the tool robocopy from the NT resource kit should do this job best with least effort.
It has switches to copy entire folders (with subfolders), and copying only newer files...

You can run this tool from vb (using shell command)...
Hope this helps
Avatar of uucutech
uucutech

ASKER

Since you're suggesting robocopy shouldn't i just use xcopy instead? Also, what would be the syntax if i wanted to run an external program, such as a batch file, with the xcopy command if i were going to go this route?
xcopy, robocopy, whatever. I suggested that tool because I have good experiences with it, and it does exactly what you request. I don't like to reinvent the wheel.

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "robocopy.exe /flagswhatever"
Dim fso
set fso = Createobject("Scripting.FileSystemObject")
fso.CopyFolder  "D:\iosafe\*", "D:\lala\",True
set fso = nothing
Something like this mya do it:

    Dim oFSO, oSourceFolder, oTargetFolder
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oSourceFolder = oFSO.GetFolder("\\server\share\pathto\source")
    Set oTargetFolder = oFSO.GetFolder("x:\pathto\target")

    RecursiveCopy(oSourceFolder, oTargetFolder, False)

    Sub RecursiveCopy(ByRef oSource, ByRef oTarget, ByVal bOverWrite)

        Dim oFolder, oFile, szPath

        For Each oFile In oSource.Files
            szPath = Replace(oTarget.Path & "\" & oFile.Name, "\\", "\")
            If Not(oFSO.FileExists(szPath) And bOverWrite) Then oFile.Copy szPath, bOverWrite
        Next

        For Each oFolder In oSource.Subfolders
            szPath = Replace(oTarget.Path & "\" & oFolder.Name, "\\", "\")
 
            If Not(oFSO.FolderExists(szPath) And bOverWrite) Then
                Set oNewTarget = oFSO.CreateFolder(szPath)
            Else
                Set oNewTarget = oSO.GetFolder(szPath)
            End If
 
            RecursiveCopy(oFolder, oTarget, bOverWrite)
        Next

    End Sub

HTH

J.
I've put this into my login script:
Dim obshell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "xcopy U:\reg\Symforms C:\Program Files\Symitar\SFW\Forms /D /S /C /Y"

but it's not actually copying anything.  When I use xcopy i have to put quotations around the source and destination for it to work correctly ex:
"U:\reg\Symforms"  "C:\Program Files\Symitar\SFW\Forms"

but when i try this in the vbscript i get an error saying expected end statement at the begining of "U:\reg\Symforms"
Robocopy does a good job on directory synchronization, so I also vote for it in this case.

It does a bit more for you than xcopy, for example:
- it can monitor directories for changes and can sync automatically
- it supports true synchronization, means that it can delete files also if they do not exist in the source any longer
- it can check whether the target already exists and is the same and skips copying the file

I want just to show not everything must be re-coded ...
but if I'm going to use robocopy i need to push it out to all the machines as well don't I?  I'm not really sure i want to do that.
You could put robocopy on a public share...?
JimBob your solution works great......will this only copy the files if they have been modified or if there are new?
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks jimbob...yours is the only one that worked with hardly any tweaking so im giving you the points