Solved

VBscript error handling and event-viewer logging

Posted on 2007-04-02
4
5,940 Views
Last Modified: 2008-02-24
I am using this vbscript to copy files form one local drive to a network drive:

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolder:Set objFolder=objFSO.GetFolder("C:\local_data\")
For Each file In objFolder.Files
  If Right(file.Name,7) = "_15.dat" Then objFSO.CopyFile file.path, "T:\remote_data\"
Next

How can I add "error handling" to the code. If there is an error I would like to write to the event-viewer, and prevent the code from crashing.

Thanks
0
Comment
Question by:novice12
  • 3
4 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18839944
Something like this would log successful copies...

Const EVENT_SUCCESS = 0
On Error Resume Next
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell = Wscript.CreateObject("Wscript.Shell")
Dim objFolder:Set objFolder=objFSO.GetFolder("C:\local_data\")
For Each file In objFolder.Files
  If Right(file.Name,7) = "_15.dat" Then
    objFSO.CopyFile file.path, "T:\remote_data\"
    If Err.Number=0 Then
       objShell.LogEvent EVENT_SUCCESS, file.path & " copied successuflly."
    Else
       objShell.LogEvent Err.Number, file.path & " failed to copy."
    End If
Next
0
 

Author Comment

by:novice12
ID: 18840007
That does it.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18840009
Happy to help - thanx for the grade! :^)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18840028
Sorry, the error logging would be incorrect there...
Use this version instead...

const SUCCESS = 0
const WARNING = 2
On Error Resume Next
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell = Wscript.CreateObject("Wscript.Shell")
Dim objFolder:Set objFolder=objFSO.GetFolder("C:\local_data\")
For Each file In objFolder.Files
  If Right(file.Name,7) = "_15.dat" Then
    objFSO.CopyFile file.path, "T:\remote_data\"
    If Err.Number=0 Then
       objShell.LogEvent SUCCESS, file.path & " copied successuflly."
    Else
       objShell.LogEvent WARNING, file.path & " failed to copy."
    End IF
Next
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now