Solved

VBscript error handling and event-viewer logging

Posted on 2007-04-02
4
5,930 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
Comment Utility
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
Comment Utility
That does it.
0
 
LVL 67

Expert Comment

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

Expert Comment

by:sirbounty
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …

762 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

12 Experts available now in Live!

Get 1:1 Help Now