Avatar of Sudhanshum
Sudhanshum
 asked on

Create Logs while transferring folder from one server to another server through vbscript

Hi I have one folder present on my server and I have created script to transfer that folder everyday to different server,What i am looking is that while transferring it should maintain logs and if any error comes then i want to send email to me ,So i should be aware that transfer got failed.Below is script I am using,Please help.

Public Str4
 Public topath
 Public Frompath
 Public Filename

'Values of variables set
strDriveLetter = "Z:"
strRemotePath = "\\192.168.1.20\dumpsDB1"
strUser = "User"
strPassword = "User"
strProfile = "false"


' ************** Setting Network Drive **************

Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

 
' ************** System Parameters **************


 Frompath= strDriveLetter
 Filename="DB1database.dmp"
 topath="c:\backup\DB1Folder"



Str4= "cmd /C copy " &  Frompath &"\"&Filename &" " & topath 
'msgbox Str4


   
	On Error Resume Next
	Set oShell = CreateObject("WScript.Shell")
	retval = oShell.Run( Str4, 1, True)
	Set oShell = Nothing

' ************** Deleting Network Drive **************

objNetwork.RemoveNetworkDrive strDriveLetter

Open in new window

VB ScriptVisual Basic.NET

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

I would suggest using the filesystem object to do the copy in VBS rather than shelling to a command line.  It's easy then to trap for errors on the copy and take action.  Below is a sample of that code, without the email logic, I'll find an example of that but you would need to specify more information about your environment.  For example, do you have an outgoing SMTP mail server that can be used to send the email?  Does it require authentication credentials?  Etc...

Public Str4
Public topath
Public Frompath
Public Filename

'Values of variables set
strDriveLetter = "Z:"
strRemotePath = "\\192.168.1.20\dumpsDB1"
strUser = "User"
strPassword = "User"
strProfile = "false"

' ************** Setting Network Drive **************
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, strProfile, strUser, strPassword

' ************** System Parameters **************
Frompath = strDriveLetter
Filename = "DB1database.dmp"
topath = "c:\backup\DB1Folder"

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile Frompath & "\" & Filename, topath
If Err.Number <> 0 Then
    ' Add error handling code here to send email, etc
End If

' ************** Deleting Network Drive **************
objNetwork.RemoveNetworkDrive strDriveLetter

Open in new window

~bp
Bill Prew

As far as the email part goes there are a lot of examples of that, here is a prior question on Experts Exchange that you can pull the email part from:


A couple of other small examples:


And a site with more comprehensive information for different situations.  I would start reading after the text "Sending a text email using a remote server.":


~bp
Sudhanshum

ASKER
Hi Bill, Thanks for reply. Please tell me where log file is creating.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Sudhanshum

ASKER
Hi Bill I understand email part but I want to create log file also while transfer,Is it possible? Please reply asap.
Sudhanshum

ASKER
Hi Bill,
I tried ur code and its always going inside error condition,that means its not copying file and alerting that error. I am not sure why error coming because through previous code,copy working fine. Plz help
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.