file-iolotusscript

Check if a directory exists with LotusScript


This seems like the most basic of things. There are lots of examples on google, all of which I have put into my code and have gotten the same result.

I beleive I am missing something specific to the language, and it's really getting irritating.

Given

pathName$ = "..\..\images\" + artID + "\" + artNum + "\"
    dirTest$ = "..\..\images\" + artID + "\"
    If Dir$(pathName$ , ATTR_DIRECTORY) = "" Then
        MsgBox "No Dir"
    Else
        MsgBox "Dir Found!"
    End If

(everthing is dimmed correctly)

I have put msgbox's before pathName$ and right before the DIR call, but it fails when it gets to the test. I know for a fact that the dir doesn't exist in certain scenarios, but I would like to trap the error, not have the script crash on failing to find the dir.

I have tried DIR (path,16) DIR$(path,16) DIR (path$,16) DIR$(path$,16) as well as the ATTR_DIRECTORY key word.

How can I gracefully check the existence of a directory in Lotusscript?


Solution

  • The Dir$ command will generate the run-time error code 76 if the directory does not exist. So you can trap the run-time error by adding On Error 76 Resume Next to your code:

    pathName$ = "..\..\images\" + artID + "\" + artNum + "\"
    dirTest$ = "..\..\images\" + artID + "\"
    On Error 76 Resume Next
    If Dir$(pathName$ , ATTR_DIRECTORY) = "" Then
        MsgBox "No Dir"
    Else
        MsgBox "Dir Found!"
    End If
    

    Inspiration: http://searchdomino.techtarget.com/tip/Finding-files-and-directories-with-LotusScript