Solved

VBscript error handling and event-viewer logging

Posted on 2007-04-02
4
5,961 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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