LOTUSSCRIPT QUICK TAKE: DIR() FUNCTION NOT REENTRANT

Just a quick tip/reminder to anyone out there who may have had to use the LotusScript (nee VisualBasic) Dir() or Dir$() function. The function IS NOT reentrant. If, as is a typical use-case, you want to deep traverse a directory tree by recursively calling a function that examines a directory using the Dir() function, take care not to nest calls within the recursion. For example:

  1. Function traverse(searchMe$)
  2. dirName$ = Dir$(searchMe$, ATTR_DIRECTORY%)
  3. Do While dirName$ <> ""
  4. If Not(dirName$ = ".") And Not(dirName$ = "..") Then
  5. If Getattr(searchMe$ & delim$ & dirName$) = ATTR_DIRECTORY% then traverse(searchMe$ & delim$ & dirName$)
  6. End If
  7. dirName$ = Dir$()
  8. Loop
  9. 'do something
  10. print dirName$
  11. End Function

Would be incorrect as the nested Dir call would advance the pointer for the caller’s Dir(). The correct usage to perform this task would be to build the complete list before making the recursive call, like so:

  1. Function traverse(searchMe$)
  2. dirName$ = Dir$(searchMe$, ATTR_DIRECTORY%)
  3. Dim dirList List As Integer
  4. Do While dirName$ <> ""
  5. If Not(dirName$ = ".") And Not(dirName$ = "..") Then
  6. If Getattr(searchMe$ & delim$ & dirName$) = ATTR_DIRECTORY% Then dirList(dirName$) = 1
  7. End If
  8. dirName$ = Dir$()
  9. Loop
  10. 'now we can loop and recurse through the list
  11. Forall de In dirList
  12. Call traverse(searchMe$ & delim$ & Listtag(de))
  13. End Forall
  14. 'do something
  15. Print searchMe$
  16. End Function

Note to Lotus: While this may be obvious to some, it wouldn’t hurt to document this in the Designer help file. Also a quick shout out to Jeff Dayton for reminding me about the runtime security level setting in the security tab of the Agent Infobox. What can I say? I get old, I forget things.

Also, check out David Leedy’s latest episode (#5) of his entertaining and informative Notes in Nine screencasts. It’s about time the LotusScript editor got some love!