Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy directory and overwrite

Status
Not open for further replies.
Sep 7, 2009
29
US
Hello, I'm trying to write a script that will copy a whole directory of folders to another location and overwrite the files in the destination folder. The problem I have now is that this script will only copy one file or one folder. I need to be able to copy a directory full of backups
Can someone help me. Here is my script

Option Explicit
Dim objNetwork,strUser,strPassword,strPath, strDriveLetter
Dim ObjFso
Dim WshNetwork,oDrives,i
Dim StrSourceFolder
Dim StrDestinationFolder

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set objNetwork = WScript.CreateObject("WScript.Network")

For i = oDrives.Count-2 To 0 Step -2
If oDrives.Item(i) = "S:" Then
WshNetwork.RemoveNetworkDrive oDrives.Item(i),True,True
End If
Next

strDriveLetter = "S:"
strPath = "\\******\BackUps"
StrSourceFolder = "\\*******\BackUps\trk\backup"
StrDestinationFolder = "H:\vbscripts\backup"
strUser = "******\******"
strPassword = "******"
objNetwork.MapNetworkDrive strDriveLetter,strPath

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFile StrSourceFolder, StrDestinationFolder'true
wscript.echo "Copy Complete"
WScript.Quit

 
You could try something like this:
Code:
Set Network=WScript.CreateObject("Wscript.Network")
Set fso=WScript.CreateObject("Scripting.FileSystemObject")
Set shell=WScript.CreateObject("Wscript.Shell")
Network.MapNetworkDrive "W:", "\\*******\groupA$"

fso.CopyFolder "C:\Program Files\Weight and Balance\AWBS\Data", "W:\AWBS\AWBS\Data", True
fso.CopyFolder "C:\Program Files\Weight and Balance\AWBS Form F\Data", "W:\AWBS\AWBS Form F\Data", True
fso.CopyFolder "C:\Program Files\Weight and Balance\Tactical Form F\Data", "W:\AWBS\Tactical Form F\Data", True
fso.CopyFolder "C:\Program Files\Weight and Balance\Transport Form F\Data", "W:\AWBS\Transport Form F\Data", True
shell.popup "Weight & Balance Data Transfer Completed on " & date(), 60, "INFO", 0+64

Network.RemoveNetworkDrive "W:"
 
I would think using objFSO.CopyFolder would be a more suitable method of accomplishing this. However, there are conditions that wil make this method succeed or fail:

If destination does not exist, the source folder and all its contents gets copied. This is the usual case.

· If destination is an existing file, an error occurs.

· If destination is a directory, an attempt is made to copy the folder and all its contents. If a file contained in source already exists in destination, an error occurs if overwrite is False. Otherwise, it will attempt to copy the file over the existing file.

· If destination is a read-only directory, an error occurs if an attempt is made to copy an existing read-only file into that directory and overwrite is False.

· An error also occurs if a source using wildcard characters doesn't match any folders.

The CopyFolder method stops on the first error it encounters. No attempt is made to roll back any changes made before the error occurs.

For a more complete reference:

-Geates
 
Thanks for the help guys. My other issue with my script is that its not authenicating the account thats in it. I had to login to the remote server first so that the account would cache before the script even worked. Is there a way I can rewrite the login function in my script so that it can automatically authenticate and connect to the drive.
 
I've never run into the problem. Check to make sure your LDAP group is a member of the administrators group on the remote machine. Also, if you query the remote WMI object, I think you can pass the executers credentials using impersonation.

set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

-Geates

 
I agree with Geates as far as credentials. However, if I do have to use passwords in a vbscript, I use PrimalScript to convert it into an executable for security reasons.
 
I added this to my script set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Now I'm getting these errors
Line 9 Char 1 Error 0x80041021 Code 80041021

Here is my code

Option Explicit
Dim objNetwork,strUser,strPassword,strPath, strDriveLetter
Dim ObjFso
Dim WshNetwork,oDrives,i
Dim StrSourceFolder
Dim StrDestinationFolder
Dim strComputer

set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set objNetwork = WScript.CreateObject("WScript.Network")

For i = oDrives.Count-2 To 0 Step -2
If oDrives.Item(i) = "S:" Then
WshNetwork.RemoveNetworkDrive oDrives.Item(i),True,True
End If
Next

strDriveLetter = "S:"
strPath = "\\********\BackUps"
StrSourceFolder = "\\********\BackUps\trk\backup"
StrDestinationFolder = "C:\backup"
strUser = "server\username"
strPassword = "********"
objNetwork.MapNetworkDrive strDriveLetter,strPath

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFolder StrSourceFolder, StrDestinationFolder'true




wscript.echo "Copy Complete"

WScript.Quit
 
Code:
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & [b]strComputer[/b] & "\root\cimv2")
contains the variable 'strComputer'. This needs to be defined.

Code:
strComputer = "."  'local computer

-Geates
 
No, Geates means that strComputer needs a value. He gave you the line to add.
Code:
strComputer = "."  'local computer
The value "." indicates the local computer.
 
Ok, I added it and I'm still getting the same error. Here is the revised code


Option Explicit
Dim objNetwork,strUser,strPassword,strPath, strDriveLetter
Dim ObjFso
Dim WshNetwork,oDrives,i
Dim StrSourceFolder
Dim StrDestinationFolder
Dim strComputer

set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set objNetwork = WScript.CreateObject("WScript.Network")

For i = oDrives.Count-2 To 0 Step -2
If oDrives.Item(i) = "S:" Then
WshNetwork.RemoveNetworkDrive oDrives.Item(i),True,True
End If
Next
strComputer = "."
strDriveLetter = "S:"
strPath = "\\********\BackUps"
StrSourceFolder = "\\********\BackUps\trk\backup"
StrDestinationFolder = "C:\backup"
strUser = "server\username"
strPassword = "********"
objNetwork.MapNetworkDrive strDriveLetter,strPath

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFolder StrSourceFolder, StrDestinationFolder'true




wscript.echo "Copy Complete"

WScript.Quit
 
Ummm... strComputer really needs a value assigned before you execute this line:
Code:
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
doesn't really work if you define it later...
 
Ok, I revised the code again and the script isn't erroring out, but its still not authenticating. I'm getting Line 27 Char 1 Error Logon failure:unknown user name or bad password.

Option Explicit
Dim objNetwork,strUser,strPassword,strPath, strDriveLetter
Dim ObjFso
Dim WshNetwork,oDrives,i
Dim StrSourceFolder
Dim StrDestinationFolder
Dim strComputer
Dim objWMI

strComputer = "."
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set objNetwork = WScript.CreateObject("WScript.Network")
set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


For i = oDrives.Count-2 To 0 Step -2
If oDrives.Item(i) = "S:" Then
WshNetwork.RemoveNetworkDrive oDrives.Item(i),True,True
End If
Next
strComputer = "."
strDriveLetter = "S:"
strPath = "\\********\BackUps"
StrSourceFolder = "\\********\BackUps\trk\backup"
StrDestinationFolder = "C:\backup"
strUser = "server\username"
strPassword = "********"
objNetwork.MapNetworkDrive strDriveLetter,strPath

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFolder StrSourceFolder, StrDestinationFolder'true




wscript.echo "Copy Complete"

WScript.Quit
 
Try
Code:
objNetwork.MapNetworkDrive strDriveLetter, strPath, False, strUser, strPassword
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top