WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: keslaa on November 22, 2016, 06:06:55 AM

Title: Error Handling Within a UDF
Post by: keslaa on November 22, 2016, 06:06:55 AM
I am using a #DefineFuntion to remove a directory after an uninstall of an application. On about 5% of the machines, Error 1030 is thrown, for various reasons. I want to be able to catch and log that error like I do for the rest of the script. Since this is a #DefineFuntion, how do I go about it? For the rest of the script, I am using IntControl 73 to go to a subroutine which logs the error into a text file before continuing on with the script. Should I add a separate IntControl 73 and corresponding WBERRORHANDLER within the #DefineFuntion? Or should I use a #DefineSubroutine instead as that might be able to utilize the existing IntControl 73?
Title: Re: Error Handling Within a UDF
Post by: td on November 22, 2016, 06:36:03 AM
From the IntControl 73 documentation in the Consolidated WIL Help file:

"If you want to have error handling occur with in a User Defined Function / User Defined Subroutine, then IntControl 73 must be defined with in the User Defined Function/User Defined Subroutine."

Title: Re: Error Handling Within a UDF
Post by: keslaa on November 22, 2016, 06:55:19 AM
Quote from: td on November 22, 2016, 06:36:03 AM
From the IntControl 73 documentation in the Consolidated WIL Help file:

"If you want to have error handling occur with in a User Defined Function / User Defined Subroutine, then IntControl 73 must be defined with in the User Defined Function/User Defined Subroutine."

Thank you. I just saw this as well. Maybe reading everything before I post a question would be better...
Title: Re: Error Handling Within a UDF
Post by: keslaa on November 22, 2016, 09:03:39 AM
I am testing this and am now receiving a new error. When trying to close the log file, it is returning the following error:
     3057: Variable could not be converted to a valid number
The line that trips this is

FileClose(LogF)

...where "LogF" is the name of the file. The file opens fine and is written to. It just doesn't like this command.

This is the full code:

#DefineFunction UDFDELTREE(dir,LogF)
IntControl(5,1,0,0,0); Include hidden files
IntControl(73, 2, 0, 0, 0)
section = "UDFDELTREE"
DirChange(dir); change to target directory
topdir=DirGet(); Delete all files in target
If FileItemize("*.*") <> ""
FileAttrSet("*.*","rash")
FileDelete("*.*")
endif
dirlist=DirItemize("*.*"); Get list of subdirectories
dircount=ItemCount(dirlist,@TAB);Process list
For xx=1 To dircount
thisdir=ItemExtract(xx,dirlist,@TAB)
UDFDELTREE(thisdir)
Next
DirChange(".."); bump up one directory level
DirAttrSet(dir,"rash"); remove that directory
DirRemove(dir)

;======================================================
:WBERRORHANDLER
error = LastError()
FunctionHandle = FileOpen(LogF,"APPEND")
text = "A bunch of lines of text regarding the error"
FileWrite(FunctionHandle, text)
FileClose(LogF)
IntControl(73, 2, 0, 0, 0)
Return; WBERRORHANDLER
;======================================================
#EndFunction
Title: Re: Error Handling Within a UDF
Post by: td on November 22, 2016, 12:59:46 PM
No sense in rewriting what has already been written so from the Consolidated WIL Help file:

"FileClose
Closes a file.

Syntax:
FileClose (filehandle)

Parameters:
(i) filehandle same integer that was returned by FileOpen."
Title: Re: Error Handling Within a UDF
Post by: keslaa on November 30, 2016, 05:18:07 AM
Should be FileClose(FunctionHandle).

Wow. What an idiot. I hate it when I don't see the obvious. Sorry to waste your time.