Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBscript error handling and event-viewer logging

Posted on 2007-04-02
4
Medium Priority
?
5,971 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

927 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