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!
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!
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.Shel l")
objShell.Run "robocopy.exe /flagswhatever"
Dim objShell
Set objShell = CreateObject("WScript.Shel
objShell.Run "robocopy.exe /flagswhatever"
Dim fso
set fso = Createobject("Scripting.Fi leSystemOb ject")
fso.CopyFolder "D:\iosafe\*", "D:\lala\",True
set fso = nothing
set fso = Createobject("Scripting.Fi
fso.CopyFolder "D:\iosafe\*", "D:\lala\",True
set fso = nothing
Something like this mya do it:
Dim oFSO, oSourceFolder, oTargetFolder
Set oFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set oSourceFolder = oFSO.GetFolder("\\server\s hare\patht o\source")
Set oTargetFolder = oFSO.GetFolder("x:\pathto\ target")
RecursiveCopy(oSourceFolde r, 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(szPa th) 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.
Dim oFSO, oSourceFolder, oTargetFolder
Set oFSO = CreateObject("Scripting.Fi
Set oSourceFolder = oFSO.GetFolder("\\server\s
Set oTargetFolder = oFSO.GetFolder("x:\pathto\
RecursiveCopy(oSourceFolde
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
Next
For Each oFolder In oSource.Subfolders
szPath = Replace(oTarget.Path & "\" & oFolder.Name, "\\", "\")
If Not(oFSO.FolderExists(szPa
Set oNewTarget = oFSO.CreateFolder(szPath)
Else
Set oNewTarget = oSO.GetFolder(szPath)
End If
RecursiveCopy(oFolder, oTarget, bOverWrite)
Next
End Sub
HTH
J.
ASKER
I've put this into my login script:
Dim obshell
Set objShell = CreateObject("WScript.Shel l")
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"
Dim obshell
Set objShell = CreateObject("WScript.Shel
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 ...
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 ...
ASKER
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...?
ASKER
JimBob your solution works great......will this only copy the files if they have been modified or if there are new?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks jimbob...yours is the only one that worked with hardly any tweaking so im giving you the points
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