Breaking out of if....endif

Started by mcvpjd3, February 10, 2015, 12:41:16 AM

Previous topic - Next topic

mcvpjd3

I've got a script which works fine most of the time, however, it checks on the network for a file and if the file has changed it needs to do stuff, if there is no change then it goes to sleep for about 30 minutes and checks again. As part of the script I check to make sure the file exists as sometimes a user might undock their laptop and take it home and the script will keep running. If the network file can't be seen then it goes into the sleep for the 30 minutes before checking for the file again.

This works mostly, but I have a few users who rarely reboot their PC and the script fails because of the dropping out of the if...endif loop (example code below) when the network file doesn't exist. Whats the best way for handling something like this?

Example code:

:START

if !fileexist(mynetworkfile)
do something
goto SLEEP
endif

if fileexist(mynetworkfile) then stuff happens here

:SLEEP

for i = timer to 0 by -1
keypress=intcontrol(1007,0,"","","")
   if keypress == 1 then gosub CHANGE
timedelay(1)
next timer

goto START

:CHANGE
do stuff
return


Thanks

td

Presumably you are getting a error 3353  'Nesting of Structure too complex'.  You can't us a 'goto' to cross a structure boundary without burning through the available slots in the structure stack and eventually getting the resulting 3353 error.  So you need to rewrite your script to avoid using a 'goto' from within the structure block.  There are many ways to rewrite your script including using an 'if-else-endif' constructs instead of   'if-endif' or using user defined subroutines/functions calls to replace where you now execute the 'SLEEP' and 'CHANGE' functionality.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

The easiest way is just to put the command to execute on the same line as the "IF', with a "THEN" before it.  THEN goto on the next line.

Like this:

IF ââ,¬Â¦ THEN do_something
THEN goto Somewhere

If you do it like this, it doesn't eat up slots.
(note that there is no ENDIF needed)

mcvpjd3

Thanks for all the replies.

The UDF route is probably the the route I'll look at using a lot more of in the future.

I've done a quick test of the the if...then...then option and that seems to have fixed the issue I have now so that's what I'll be doing for this script at the moment - If I need to re-write I'll look at not using Goto at all.

Regards