Solved

VBscript error handling and event-viewer logging

Posted on 2007-04-02
4
5,958 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 …

821 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