email notification using vbscript

hi all,

im trying to modify a script that i founf on the net that will send me latest ntbackup logs and im getting the following error.
Im running windows 2003 server

Windows Script Host
Script: C:\Batch\es_email_ntbackup_log.vbs
Line: 101
Char: 8
Error: The System cannot find the file specified
Code: 80070002
Source: (null)


Thanks

this is the script




' Author: NETIKUS.NET ltd

' Date:   9/27/2006

' Title:  Email NTBackup Log file using blat.exe

' Description: Emails the latest ntbackup.exe log file using blat.exe, and optionally compresses the attachment

 

Dim SmtpServer

Dim EmailSender, EmailRecipient, EmailSubject

 

Dim aFile

Dim fNewest

Dim oFolder

Dim LogFolder

Dim numFiles

 



Dim EmailClient

 

' Set your preferences here *

SmtpServer                = "smtp.xtra.co.nz"

EmailSender                = "airport@aucklandnz.com"

EmailRecipient        = "chris@aucklandnz.com"

EmailSubject        = "NTBackup Log File"

LogFolder                = "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"


EmailClient                = "C:\Batch\blat.exe"

 

Set WshShell        = WScript.CreateObject("WScript.Shell")

Set oFolder                = CreateObject("Scripting.FileSystemObject").GetFolder(LogFolder)

 

numFiles = 0

 

' Find log file with latest time stamp

For Each aFile In oFolder.Files

       If Right(aFile.Name, 4) = ".log" Then

               numFiles = numFiles + 1

         

               If fNewest = "" or fNewest = null Then

                       Set fNewest = aFile

               Else

                       If fNewest.DateLastModified < aFile.DateLastModified Then

                               Set fNewest = aFile

                       End If

               End If

       End If

Next

 



 

' Email log file

If numFiles > 0 Then


       WshShell.Run EmailClient & " - -body ""NTBackup Log File"" -server " & SmtpServer & " -f " & EmailSender & " -subject " & chr(34) & EmailSubject & chr(34) & " -to " & EmailRecipient, 3, 1

End If

Open in new window

LVL 3
aucklandnzAsked:
Who is Participating?
 
MegaNuk3Commented:
try this code (I have tested it this time ;-)   ) and it will send the 2 most recent files.

run it from command line with "cscript files_ee.vbs" otherwise you will get popup windows (Message boxes) for all the places I have put wscript.echo in...
' Author: NETIKUS.NET ltd
' Date:   9/27/2006
' Title:  Email NTBackup Log file using blat.exe
' Description: Emails the latest ntbackup.exe log file using blat.exe, and optionally compresses the attachment

Dim SmtpServer
Dim EmailSender, EmailRecipient, EmailSubject
Dim aFile
Dim fNewest
Dim oFolder
Dim LogFolder
Dim numFiles
Dim EmailClient

' Set your preferences here *

SmtpServer                = "smtp.mydomain.com"
EmailSender                = "email@mydomain.com"
EmailRecipient        = "chris@mydomain.com"
EmailSubject        = "NTBackup Log File"
LogFolder                = "C:\log"
Set WshShell        = WScript.CreateObject("WScript.Shell")
Set oFolder                = CreateObject("Scripting.FileSystemObject").GetFolder(LogFolder)

numFiles = 0

' Find log file with latest time stamp

For Each aFile In oFolder.Files
  wscript.echo "Checking " & afile
  If LCase(Right(aFile.Name, 4)) = ".log" Then
    numFiles = numFiles + 1        
    If fNewest = "" or fNewest = null Then
      Set fNewest = aFile
      wscript.echo fNewest.DateLastModified
    ElseIf fNewest.DateLastModified < aFile.DateLastModified Then
      Set f2ndNewest = fNewest
      wscript.echo f2ndNewest
      Set fNewest = aFile
      wscript.echo "Newest file: " & fNewest
    Else
      If f2ndNewest = "" OR f2ndNewest = null Then
        Set f2ndNewest = aFile
      ElseIf f2ndNewest.DateLastModified < aFile.DateLastModified Then 
        Set f2ndNewest = aFile
        wscript.echo f2ndNewest
      End If    
    End If
  End If
Next
wscript.echo "============================"
wscript.echo "Newest: " & fNewest
wscript.echo "2nd Newest: " & f2ndNewest

' Email log file
If numFiles > 0 Then
       Set objMessage = CreateObject("CDO.Message") 
       objMessage.Subject = EmailSubject
       objMessage.Sender = EmailSender
       objMessage.To = EmailRecipient
       objMessage.TextBody = "NTBackup Log File"
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
       objMessage.Configuration.Fields.Update
       objMessage.AddAttachment fNewest 
       objMessage.AddAttachment f2ndNewest    
       objMessage.Send
       wscript.echo "============================"
       wscript.echo "Message Sent"
       set objMessage = Nothing
End if

Open in new window

0
 
rhinocerosCommented:
The problem should come from your LogFolder path
(LogFolder = "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data" )

Do you know vbscript (MS-DOS) syntax ? It will not accept "SPACE" in folder path name.

C:\DOCUME~1>cd Administrator
C:\DOCUME~1\Administrator>cd\
C:\>cd\docume~1\
C:\DOCUME~1>cd Administrator
C:\DOCUME~1\Administrator>cd "local settings"
C:\DOCUME~1\Administrator\Local Settings>

At First, you should change path (e.g. C:\LogFolder") to TEST your batch is worked or not ?

If worked, and then try it to change it as following
c:\doume~1\Administrator\Locals~1\Applic~1\Microsoft\Window~1\Ntbackup\Data

Or even copy the log file to one STANDARD folder name (e.g. not more than 8 characters) for your script running.
0
 
MegaNuk3Commented:
Maybe write all of line 101 into a string like:
sCommand = EmailClient & " - -body ""NTBackup Log File"" -server " & SmtpServer & " -f " & EmailSender & " -subject " & chr(34) & EmailSubject & chr(34) & " -to " & EmailRecipient, 3, 1

wscript.echo sCommand

does sCommand contain the relevant info for Blat? If you copy that line it echo'd out and run it, does the message get sent?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
aucklandnzAuthor Commented:
Im getting the following error:

Windows Script Host
Script: C:\Batch\es_email_ntbackup_log.vbs
Line: 101
Char: 188
Error: Expected end of statement
Code: 800A0401
Source: Microsoft VBScript compilation error

I copied logs to c:\log

i have changed script to it to :
' Author: NETIKUS.NET ltd

' Date:   9/27/2006

' Title:  Email NTBackup Log file using blat.exe

' Description: Emails the latest ntbackup.exe log file using blat.exe, and optionally compresses the attachment

 

Dim SmtpServer

Dim EmailSender, EmailRecipient, EmailSubject

 

Dim aFile

Dim fNewest

Dim oFolder

Dim LogFolder

Dim numFiles

 



Dim EmailClient

 

' Set your preferences here *

SmtpServer                = "smtp.mydomain.com"

EmailSender                = "email@mydomain.com"

EmailRecipient        = "chris@mydomain.com"

EmailSubject        = "NTBackup Log File"

LogFolder                = "C:\log"


EmailClient                = "C:\Batch\blat.exe"

 

Set WshShell        = WScript.CreateObject("WScript.Shell")

Set oFolder                = CreateObject("Scripting.FileSystemObject").GetFolder(LogFolder)

 

numFiles = 0

 

' Find log file with latest time stamp

For Each aFile In oFolder.Files

       If Right(aFile.Name, 4) = ".log" Then

               numFiles = numFiles + 1

         

               If fNewest = "" or fNewest = null Then

                       Set fNewest = aFile

               Else

                       If fNewest.DateLastModified < aFile.DateLastModified Then

                               Set fNewest = aFile

                       End If

               End If

       End If

Next

 



 

' Email log file

If numFiles > 0 Then


       sCommand = EmailClient & " - -body ""NTBackup Log File"" -server " & SmtpServer & " -f " & EmailSender & " -subject " &  chr(34) & EmailSubject & chr(34) & " -to " & EmailRecipient, 3, 1

       wscript.echo sCommand

End If

Open in new window

0
 
rhinocerosCommented:
sCommand = EmailClient & " - -body ""NTBackup Log File"" -server " & SmtpServer & " -f " & EmailSender & " -subject " &  chr(34) & EmailSubject & chr(34) & " -to " & EmailRecipient, 3, 1

Have you TESTED blat.exe syntax ? e.g. "- - body" ?

Good example:
WshShell.Run "blat -to backups@myemail.com -subject ""test 1 2 3"" -body ""test 1 2 3""", 1, True
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23278728.html

On the other hand, I often send email with The CDO Message Object, not use other third party software.
Sample:
     If numFiles > 0 Then
       Set objMessage = CreateObject("CDO.Message")
       objMessage.Subject = "NTBackup Log File"
       objMessage.Sender = "email@mydomain.com"
       objMessage.To = "chris@mydomain.com"
       objMessage.TextBody = "NTBackup Log File"
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mydomain.com"
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
       objMessage.Configuration.Fields.Update
       objMessage.Send
       set objMessage = Nothing
       End if
0
 
MegaNuk3Commented:
your line isn't working cause:

& EmailRecipient, 3, 1

should be something like
& EmailRecipient & ", 3, 1"

0
 
MegaNuk3Commented:
Instead of using blat use vbscript to send the message as rhinoceros said...

Sending Mail with attachment:
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me@mydomain.com"
objMessage.To = "test@mydomain.com"
objMessage.TextBody = "This is some sample message text."
objMessage.AddAttachment "c:\temp\readme.txt"
objMessage.Send
0
 
MegaNuk3Commented:
Or just add this line into what rhinoceros has said already
objMessage.AddAttachment "c:\logs\readme.txt"

so yours would be something like:
sAttachment = LogFolder & "\" & fNewest
objMessage.AddAttachment sAttachment
0
 
aucklandnzAuthor Commented:
Sorry....im lost now as i dont know VBScripts

0
 
MegaNuk3Commented:
So your code would be something like the below:

Just change objMessage.Sender and ObjMessage.To to valid e-mail addresses

And change smtp.mydomain.com to a valid exchange/SMTP server
' Author: NETIKUS.NET ltd

' Date:   9/27/2006

' Title:  Email NTBackup Log file using blat.exe

' Description: Emails the latest ntbackup.exe log file using blat.exe, and optionally compresses the attachment

 

Dim SmtpServer

Dim EmailSender, EmailRecipient, EmailSubject

 

Dim aFile

Dim fNewest

Dim oFolder

Dim LogFolder

Dim numFiles

 



Dim EmailClient

 

' Set your preferences here *

SmtpServer                = "smtp.mydomain.com"

EmailSender                = "email@mydomain.com"

EmailRecipient        = "chris@mydomain.com"

EmailSubject        = "NTBackup Log File"

LogFolder                = "C:\log"


EmailClient                = "C:\Batch\blat.exe"

 

Set WshShell        = WScript.CreateObject("WScript.Shell")

Set oFolder                = CreateObject("Scripting.FileSystemObject").GetFolder(LogFolder)

 

numFiles = 0

 

' Find log file with latest time stamp

For Each aFile In oFolder.Files

       If Right(aFile.Name, 4) = ".log" Then

               numFiles = numFiles + 1

         

               If fNewest = "" or fNewest = null Then

                       Set fNewest = aFile

               Else

                       If fNewest.DateLastModified < aFile.DateLastModified Then

                               Set fNewest = aFile

                       End If

               End If

       End If

Next

 



 

' Email log file

If numFiles > 0 Then
       Set objMessage = CreateObject("CDO.Message") 
       objMessage.Subject = "NTBackup Log File"
       objMessage.Sender = "email@mydomain.com"
       objMessage.To = "chris@mydomain.com"
       objMessage.TextBody = "NTBackup Log File"
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mydomain.com"
       objMessage.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
       objMessage.Configuration.Fields.Update
       sAttachment = LogFolder & "\" & fNewest
       objMessage.AddAttachment sAttachment 
       objMessage.Send
       set objMessage = Nothing
End if

Open in new window

0
 
aucklandnzAuthor Commented:
thanks a lot for reply


im getting the following error

Line: 109
Char: 8
Error: The File Name, directory name, or volume label syntax is incorect

code: 800700B
source: CDO.Message.1


when i change sAttachment to Attachment in line 108 it send me an email but the attachment is
ATT00001..bin

anyidea?

objMessage.AddAttachment sAttachment  

i had to remove s to read Attachment not sAttachment

0
 
rhinocerosCommented:
Please change from:
 
       sAttachment = LogFolder & "\" & fNewest
       objMessage.AddAttachment sAttachment

To:
       objMessage.AddAttachment fNewest

----------------------------------------------------------------------------------------------------------
Because the procedure ' Find log file with latest time stamp' will directly generate Full Path

fNewest = "C:\log\xxx.log"

Then, it will have duplicate path when you rerun "sAttachment = LogFolder & "\" & fNewest".
0
 
aucklandnzAuthor Commented:
yeah...you are the man!!! Thank you very much


Its working fine... i have just noticed that i have 2 jobs running each day. is it possible to attach 2 newest files ?

Thanks
0
 
MegaNuk3Commented:
Or why not run the script as a post-backup process? Some backup software allows you to select a post backup script that it will run.

0
 
MegaNuk3Commented:
@rhinoceros - thanks for picking up the 'full path' bit. I must admit that I hadn't run the code myself to see that.
0
 
MegaNuk3Commented:
You may want to change line 59 from:
objMessage.Sender = EmailSender
To
objMessage.From = EmailSender

otherwise the mail will appear to have a blank from field when viewed from Outlook (before opening it)
0
 
aucklandnzAuthor Commented:
it works,

however i have to click OK many times as its checking each log and backup job, than last message is message sent and i have to click OK

sorry to be a pain.
0
 
rhinocerosCommented:
You should delete all "Wscript.Echo....." line

MegaNuk3 used "Wscript.Echo" command to display output for procedure checking, who may forget to remove it before post.
0
 
MegaNuk3Commented:
Thanks for the points.

As I said in my comment #32257982 earlier. You should run it from the command-line with "cscript <whateveryoucalledthescript>" and then those messages will just echo to command-line instead of being message popup boxes. You can create a batch (.bat) file to call the script like that.

OR just delete or comment out (with inverted commas =') the wscript.echo lines like rhinoceros said, but then you will not see what the script is doing if you are trying to troubleshoot it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.