WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: sense2k on April 13, 2015, 08:48:14 AM

Title: udf return value not available to caller
Post by: sense2k on April 13, 2015, 08:48:14 AM
Hello
I using 2011a.  modifying an existing script by adding an additional udf.  I need to use the return value (which basically is a count) as the limit in a For loop.  When I run it it says "uninitialized variable etc etc"
I have place a message(..) in the function and I know it's doing what it's supposed to do.
All my UDF's are defined before the main section.
Can someone tell me what I'm doing wrong?

Thanks
Title: Re: udf return value not available to caller
Post by: td on April 13, 2015, 09:49:27 AM
Likely cause is faulty logic in your script but since you haven't provided the logic, it is difficult to point out the fault.
Title: Re: udf return value not available to caller
Post by: td on April 13, 2015, 01:47:06 PM
Should have added that not properly closing a structure block with an 'endif', 'next', or 'endwhile' as the case my be can sometimes cause errors that seemingly defy explanation.
Title: Re: udf return value not available to caller
Post by: sense2k on April 14, 2015, 08:17:26 AM
Hello
Thx for replying.
Here is the script
I'm guessing it has to do with the way I break out of the while @true
but I don't know how to correct it
Thanks for looking at it and helping me
Title: Re: udf return value not available to caller
Post by: td on April 14, 2015, 10:13:44 AM
It is not entirely clear how much of your script is intentional as apposed to incidental but one thing is certain.  Never use a goto statement to break out of a structure of any kind.   The following script illustrates one of several possible alternatives but you would need to base your script on your actual intentions.

Code (winbatch) Select
#definefunction getddirct(ddiroot)
   ddirct=0

   ;dirchange(ddiroot)
   ;currdir=dirget()
   dirlist=diritemize(ddiroot:"*.*")

   nCount = ItemCount(dirlist, @Tab)
   for i = 1 to nCount
      dataxxxdir = ItemExtract(i, dirlist, @Tab)  ; or
      ;;dataxxxdir = ddiroot:ItemExtract(i, dirlist, @Tab) ?????
      firstfour = strindex(dataxxxdir,"data",1,@fwdscan)
      if firstfour <> 0
         ddirct=ddirct+1
      endif
   next

   return ddirct
#endfunction

;********************* M A I N ***********************

dirchange("C:\Projects")

currdir=dirget()
ddirct = getddirct(currdir)
message("ddirct is",ddirct)



Title: Re: udf return value not available to caller
Post by: sense2k on April 14, 2015, 10:20:08 AM
hi
yeah I thought it was a bad idea.  I got my value by changing the udf to a subroutine
but i'm still working on cleaning up the logic

thanks a lot
Title: Re: udf return value not available to caller
Post by: td on April 14, 2015, 11:21:59 AM
You can get the value from a user defined function by placing your function call on the right-hand side of an assignment statement with a variable on the left.  But a subroutine works too.   
Title: Re: udf return value not available to caller
Post by: sense2k on April 14, 2015, 12:04:31 PM
That's good to know
thanks for your help