Page 1 of 1 1
Topic Options
#65269 - 2002-05-07 05:06 AM Manually set @error value or . . .
Anonymous
Unregistered


Hey all,

Here's one that it seems I should be able to figure out.

I have this function that I use to clean up after a screw-up:

function catcherror($errortext)
if @error <> 0
messagebox("$errortext",48)
del(c:\temp.bat)
exit
endif
endfunction

This works just fine. However, I want to be able to use it if both if's are negative in this structure:

if exist ("c:\lotus\notes\notes.ini")
$notesini = "c:\lotus\notes\notes.ini"
else
if exist ("c:\notes\notes.ini")
$notesini = "c:\notes\notes.ini"
else
*****catcherror("Could not find your notes.ini file.")*****
endif
endif

Problem, as you've probably already noticed, is that the value of @error is still 0, and therefore does not cause the exit action.

Is there a better way of writing the IF structure so that it will produce an error value? Is there an easy clean way of forcing the @error <> 0?

Thanks,
Chaz

mccachar@consultingprof.com

Top
#65270 - 2002-05-07 05:26 AM Re: Manually set @error value or . . .
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
If you would use a UDF maybe OpenFile() it could use the EXIT command to set @error based on the return code directly from the open command.

As you have seen, you can not set @error with if exist.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#65271 - 2002-05-07 05:28 AM Re: Manually set @error value or . . .
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Charles,

I see exactly where your coming from, very nice. This kinda reminds of the old C++ try and catch type error handling. So I get the impression you want to call this function from many different places in your script, its just that the exist function isn't playing ball ?

My suggestion would be to modify the UDF to pass the value of @ERROR as a parameter - instead of just checking it "globally", example:

function catcherror($error, $errortext)
if $error <> 0
messagebox("$errortext",48)
del(c:\temp.bat)
exit
endif
endfunction

Then, for the bulk of your trapping, you can do this:

somecode...
else
catcherror(@ERROR,"Some message")
endif

and for stuff that doesn't trigger @ERROR's, you might force an error through the parm, like this:

if exist ("c:\notes\notes.ini")
$notesini = "c:\notes\notes.ini"
else
catcherror(2,"Could not find your notes.ini file.")*****
endif

Follow me on this one ?

-Shawn

Top
#65272 - 2002-05-07 05:36 PM Re: Manually set @error value or . . .
Anonymous
Unregistered


Yeah, I follow you, Shawn. For this instance, Howard's answer is more expedient (less I'll have to change in the rest of the script), but I will implement your change in the future.

It ends up reading like this (no change to catcherror):

open(1,"c:\lotus\notes\notes.ini",0)
if @error = 0
close(1)
$notesini = "c:\lotus\notes\notes.ini"
else
open(1,"c:\notes\notes.ini",0)
catcherror("Could not find your notes.ini file.")
close(1)
$notesini = "c:\notes\notes.ini"
endif

Top
#65273 - 2002-05-07 10:28 PM Re: Manually set @error value or . . .
Anonymous
Unregistered


Hmm . . .

I tried doing it your way Shawn, and it didn't work. Here's an example. It seems that there is a lack of error reporting in some Kix functions.

function catcherror($errortext, optional $error)
if $error <> 0
messagebox($errortext,"Script Failure",48)
del(c:\temp.bat)
exit
endif
endfunction

$noteslocation = readprofilestring($notesini,"Notes","Location")
catcherror("Could not determine noteslocation.",@error)

The READPROFILESTRING function doesn't return an error code if the INI entry doesn't exist.

Is there a universally better way of error checking in Kix?

Top
#65274 - 2002-05-07 10:45 PM Re: Manually set @error value or . . .
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well, the exist() thingy not returning @ERROR is by design. The readprofilestring() thingy not returning @ERROR is a known Microsoft limitation with the underlaying Windows API - its doesn't return an error code either. I think your just hung-up on a couple of odd-ball functions. For readprofilesting, i think you just have to devine an error code from the data itself. That is to say, if readprofilestring() returns a NULL string, its either because the value doesn't exist or that the INI file itself doesn't exist.

-Shawn

p.s. imho, this catcherror() udf of yours is pretty slick, has anyone come up with a better one ( other than just checking @ERROR all over the place in the mainline) ? not to my knowledge ...

[ 07 May 2002, 22:52: Message edited by: Shawn ]

Top
#65275 - 2002-05-08 12:51 AM Re: Manually set @error value or . . .
Anonymous
Unregistered


I haven't seen a UDF for it anywhere else, which surprises me, cause there are some frightenly complex things being done in Kix.

I've expanded the UDF a little, in case anyone cares to steal it:

function catcherror($errortext, $error, optional $seterror)
if $error <> 0 or $seterror <> 0
open(9,"c:\kix\@wksta.log",5)
writeline(9,"LIST OF VARS AND VALUES HERE")
close(9)
messagebox("$errortext","Script Failure",48)
del(c:\temp.bat)
$error = 0
$seterror = 0
exit
endif
endfunction

Note the addition of $seterror. This way, I can call the funciton as-is when the @error code is normal and if it's not, do some other simple test (if $var = "") and $seterror = 1.

I have another little issue here, though. I want the EXIT command to bail out of this KIX script, but all it seems to do is exit the function. Is there some other command I'm missing?

Shawn, can you give me any more background on that MS known limitation, just for shots and giggles?

Top
#65276 - 2002-05-08 04:04 AM Re: Manually set @error value or . . .
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Charles,

Background on that readprofilestring() thingy ?

I went searching on the internet looking for documentation on this, and couldn't find anything anywhere. Even at MSDN, it's more what the documentation doesn't say, than what it does say. First, here's a thread that has some comments from the developer of Kixtart:

Topic: Old, minor bug, still there.

Here's a link to the actual API calls for ReadProfileString and WriteProfileString:

GetPrivateProfileString

WritePrivateProfileString

If you look under "return code" for WritePrivateProfileString, you will see that one can use GetLastError() to get extended error info. If you look under "return code" for GetPrivateProfileString, there is no mention of GetLastError() - thats because MS designed this particular API to be pretty much bullet proof.

One of the parms to GetPrivateProfileString is a default value to be returned if an error does occur. If you provide a bad filespec or non-existent key, whatever, you'll get this default returned.

So you can trust me on this one, I've actually tried it, if you code a call to GetPrivateProfileString() in C or VB, and you provide bad data, no error will be returned and GetLastError will return 0.

In terms of the EXIT and UDFs - might try the QUIT statement - like QUIT(1) - that should do the trick.

-Shawn

Top
#65277 - 2002-05-08 07:10 AM Re: Manually set @error value or . . .
Anonymous
Unregistered


Thanks for all the extra info. I tried using QUIT, but that means that this must be the last required script in the series (or run kix32 again from the calling batch file for any other required scripts). What I ended up with was a GOTO (ugh). Works.

For the enjoyment of all, here's the full script. What we're doing is a mass renaming and describing of workstations, along with setting the workgroup, to clean up browsing. We're a Notes shop (ugh again) and the nicest way I could divine to get an accurate short name and full name for the usual user of the machine was to extract it from the notes.ini.

if exist("c:\kix\namesdone.txt") goto theend endif

charmap

getnotesini

getuserfullname

getusershortname

getnewcomputername

getnewdescription

if @inwin = 1

opentempbat

writeline(1,'net config server /srvcomment:"$newdescription" $enter')
catcherror("Could not edit temp.bat.",@error)

runandkill

else

opentempbat

writeline(1,"c:\reg.exe update hklm\system\currentcontrolset\services\vxd\vnetsup\workgroup=sas $enter")
catcherror("Could not edit temp.bat.",@error)

writeline(1,'c:\reg.exe update hklm\system\currentcontrolset\services\vxd\vnetsup\comment="$newdescription" $enter')
catcherror("Could not edit temp.bat.",@error)

writeline(1,'c:\reg.exe update hklm\system\currentcontrolset\control\computername\computername\computername="$newcomputername" $enter')
catcherror("Could not edit temp.bat.",@error)

runandkill

endif

:theend
exit

;function definitions=========================================================================

function getnotesini

open(1,"c:\lotus\notes\notes.ini",0)
if @error = 0
close(1)
$notesini = "c:\lotus\notes\notes.ini"
else
open(1,"c:\notes\notes.ini",0)
catcherror("Could not find your notes.ini file.",@error)
close(1)
$notesini = "c:\notes\notes.ini"
endif

endfunction
;----------------------------------------
function getuserfullname

$noteslocation = readprofilestring($notesini,"Notes","Location")

$noteslocationarray = split($noteslocation,"/",1)

for each $element in $noteslocationarray
$userfullname = right($element,len($element)-instr($element,"="))
next

if $userfullname = "" $seterror = 1 endif
catcherror("Could not determine userfullname.",@error,$seterror)

endfunction
;----------------------------------------
function getusershortname

$usershortnametemp1 = readprofilestring($notesini,"Notes","KeyFilename")

$usershortnamearray = split($usershortnametemp1,"\")

for each $element in $usershortnamearray
$usershortnametemp2 = $element
next

$thecount = instr($usershortnametemp2,".")
$usershortname = left($usershortnametemp2,$thecount - 1)

if $usershortname = "" $seterror = 1 endif
catcherror("Could not determine usershortname.",@error,$seterror)

endfunction
;----------------------------------------
function getnewcomputername

if $usershortname = @wksta
$newcomputername = trim(@wksta)
else
$newcomputername = trim(left($usershortname+"#"+@wksta,15))
endif

endfunction
;----------------------------------------
function getnewdescription

$newdescription = trim(left(@producttype+"#"+@csd+"#"+$userfullname,48))

endfunction
;----------------------------------------
function opentempbat

open(1,"c:\temp.bat",5)
catcherror("Could not create temp.bat.",@error)

endfunction
;----------------------------------------
function runandkill

close(1)
shell "c:\temp.bat"
catcherror("Could not run temp.bat.",@error)

del(c:\temp.bat)

open(9,"c:\kix\namesdone.txt",5)
catcherror("Could not open semaphore file.",@error)

writeline(9,"This machine's name and description have been updated.")
catcherror("Could not write to semaphore file.",@error)

close(9)

endfunction
;----------------------------------------
function catcherror($errortext, $error, optional $seterror)

if $error <> 0 or $seterror <> 0
open(9,"c:\kix\@wksta.log",5)
writeline(9,"usershortname=$usershortname $enter userfullname=$userfullname $enter noteslocation=$noteslocation $enter notesini=$notesini $enter newdescription=$newdescription $enter newcomputername=$newcomputername $enter errortext=$errortext $enter error=$error $enter seterror=$seterror $enter")
close(9)
messagebox("$errortext Please call the HelpDesk at 555-555-5555.","Script Failure",48)
del(c:\temp.bat)
$error = 0
$seterror = 0
goto theend
endif

endfunction
;----------------------------------------
function charmap

$lf=chr(13)
$cr=chr(10)
$enter=$lf+$cr

endfunction

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.055 seconds in which 0.022 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org