Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I catch external errors?

Status
Not open for further replies.

sshafe

Programmer
Oct 18, 2002
71
US
Within a script of mine, I'm kicking off an external process (defrag) and want to catch and handle any errors that the defrag might throw. Is there a way to do this?

If external error
call Error_handling

?

Thanks in advance
 
Code:
lngRetVal = oWSH.Run("some executable",1,TRUE) 'wait for exit
If lngRetVal<>0 Then
   MsgBox &quot;Error #&quot; & lngRetVal & &quot; was returned after running some program&quot;
End If

Of course, this will only work if the program you call doesn't spawn a separate process (which would return execution back to the WSH Script prematurely) and if the executed program returns back error codes upon exit.

HtH,


Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Those are actually the error messages I watch to catch. Here's the deal. I am kicking off a defrag process, (Not microsofts, or that would be easy), and I want to catch any messages it throws out, good or bad, and log them. Any ideas.

Thanks.
 
Try to check if that defrag software has some message logging and then you have to check that log.

Other way wrote your own defrag program :))

________
George, M
 
>Other way wrote your own defrag program :))

:) Not necessarily as silly as it sounds, since MS added defragmentation API's to Windows 2000 and above...

(but probably not possible to do in VBScript)
 
You make an component(ActiveX) and you can use in what script you want.

________
George, M
 
Yes, jolly good. So the defrag code won't actually be in VBScript, will it? Which was my point.
 
I'm pretty sure there is an event log COM object (or perhaps through WBEM) you can connect to through VB/J-Script. The question then becomes; Does the defrag proggie output to the event log? Otherwise, you will have to parse out a log file using the file system object (ugh!). If you want to scan and read message output to the screen you will need VB (or better) or WinBatch (or comparable) - programs that can scrap screens and/or hook into the Windows messaging layer.

Regards,

Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
With Windows Script Host 5.6 or better use the Exec method which allows you to catch the stdout and stderr streams.

I find this works better than Run.

example:

Dim oExec
Set oExec = oWsh.Exec(ExecStr)
Do While oExec.Status =0
Wscript.Sleep (100)
Loop

If oExec.ExitCode <> 0 Then (oExec.ExitCode)
WScript.Echo &quot;Warning: Non-zero exit code&quot; & vbcrlf & oExec.ExitCode
End If

'Displays stdOut
If Not oExec.StdOut.AtEndOfStream Then
strErrOut = &quot;StdOut: &quot; & oExec.StdOut.ReadAll
Wscript.echo strErrOut
End If

'Display stdErr
If Not oExec.StdErr.AtEndOfStream Then
strErrOut = &quot;ERROR &quot; & oExec.StdErr.ReadAll
Wscript.echo strErrOut
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top