Finding files and directories with LotusScript

LotusScript provides a simple Dir$ command to check for the existence of a file or directory, but there are a few wrinkles you need to know about in order to use it correctly.

VIEW MEMBER FEEDACK TO THIS TIP

A common operation from any programming language is to find the existence of a file or directory. LotusScript provides a simple command to do this, but it contains a few wrinkles that you should understand in order to use it correctly.

The built-in LotusScript command Dir$ looks for files and directories on the operating system's file system. Dir$ takes two arguments: a wildcard specification for finding files and a qualifier that controls what kind of files it searches for. The latter argument is most often used to find either regular files or directories. We'll start by looking at the simplest use of Dir$, to find files.


Example 1

Const FILE_PATH = "C:\temp\test.txt"
Dim DirResult As String
 
On Error Goto ErrorReturn
  
DirResult = Dir$ (FILE_PATH, ATTR_NORMAL)
If DirResult <> "" Then
Msgbox "Found the file named " 
+ FILE_PATH + ".", _ 
MB_OK + MB_ICONINFORMATION, "Found"
Else
 Msgbox "Could not find the file named " 
+ FILE_PATH + ".", _
MB_OK + MB_ICONINFORMATION, "Not Found"
End If
Exit Sub 
 
ErrorReturn:
Msgbox "Something went wrong. 
Error number is " + Cstr(Err) + ".", _
MB_OK + MB_ICONSTOP, "Error"
Resume ErrorReturn2
ErrorReturn2:
Exit Sub

Notice that, in this case, the Dir$ command takes a full pathname for a file, and the qualifier ATTR_NORMAL. The command simply looks for a regular file with that exact path. If the file is found, Dir$ returns just the filename portion (without the directory). If the file is not found, Dir$ returns the empty string. The If-Then-Else that follows prints an appropriate error message.

Example 1 is correct and works, whether the file is present or not in the specified directory. But this code also has a problem. If the directory itself does not exist, the Dir$ command will generate a runtime error, because Dir$ cannot search the directory for the file.

In my opinion, Dir$ should handle a missing directory more gracefully, but it does not. In order to make this code work correctly for directory errors also, we have to add special handling of runtime errors. Example 2 shows how to do so.


Example 2

Const DIR_NOT_FOUND = 76
'Const FILE_PATH = "C:\temp\test.txt" 
Const FILE_PATH = "C:\temp2\test.txt"
Dim DirResult As String
 
On Error Goto ErrorReturn

' your code here, with normal error handling
 
On Error DIR_NOT_FOUND Resume Next
DirResult = Dir$ 
(FILE_PATH, ATTR_NORMAL)
On Error Goto ErrorReturn
If DirResult <> "" Then
 Msgbox "Found the file named " + 
FILE_PATH + ".", _
MB_OK + MB_ICONINFORMATION, "Found"
Else
 Msgbox "Could not find the file named " + 
FILE_PATH + ".", _
MB_OK + MB_ICONINFORMATION, 
"Not Found"
End If
Exit Sub
 
ErrorReturn:
Msgbox "Something went wrong. 
Error number is " + Cstr(Err) + ".", _
MB_OK + MB_ICONSTOP, "Error"
Resume ErrorReturn2
ErrorReturn2:
Exit Sub

Notice that in Example 2, the Dir$ command is sandwiched between two On Error statements. The first says "For just error number 76, do not jump to the error handling routine. Instead continue processing at the next statement." Error code 76 is the error raised by the Dir$ command when the search directory does not exist. The second On Error statement tells the program to switch back to normal error handling for all error codes.

This example works like the first, except that it correctly deals with missing directories. You can test this switching the directory used in the FILE_PATH constant at the top of the code.

To complete the use of the Dir$ command, there is one more common operation we have not yet covered. Suppose you want to search for a directory rather than a file. Dir$ can do this, using the special error handling from Example 2 and a new qualifier.


Example 3

Const DIR_NOT_FOUND = 76
'Const DIR_PATH = "C:\temp\" 
Const DIR_PATH = "C:\temp2\"  
Dim DirResult As String
 
On Error Goto ErrorReturn
 
' your code here....
 
' you can see the effect of this 
special error handling 
by commenting out the next line
On Error DIR_NOT_FOUND Resume Next 
DirResult = Dir$ 
(DIR_PATH, ATTR_DIRECTORY)
On Error Goto ErrorReturn
 
If DirResult <> "" Then
 Msgbox "Found the directory named " 
+ DIR_PATH + ".", _
MB_OK + MB_ICONINFORMATION, "Found"
Else
Msgbox "Could not find the directory named " 
+ DIR_PATH + ".", _
MB_OK + MB_ICONINFORMATION, "Not Found"
End If
Exit Sub

ErrorReturn:
Msgbox "Something went wrong. 
Error number is " + Cstr(Err) + ".", _
MB_OK + MB_ICONSTOP, "Error"
Resume ErrorReturn2
ErrorReturn2:
Exit Sub

In this example, we use the ATTR_DIRECTORY qualifier to find directories as well as normal files. The remainder of the code is similar to Example 2. One thing to keep in mind about ATTR_DIRECTORY is that Dir$ always returns normal files.

If you have a directory and a file with the same name in the same location, the Dir$ command will find both of them. This situation should not arise with proper file naming conventions, but it is something to be aware of.

For more information see Domino Designer Help -> Index -> Dir Function.

About the author: Chuck Connell is president of CHC-3 Consulting, which helps organizations with all aspects of Lotus Notes and Domino.

MEMBER FEEDBACK TO THIS TIP

I agree that Dir is a pain from this perspective, but I think a better method is to use the FileSystemObject,. It is much richer in functionality and does not have the mentioned errors.
—Scott H.

******************************************

Example #3 will not find hidden directories. You should ALWAYS include add the ATTR_HIDDEN modifier to the ATTR_DIRECTORY (or even ATTR_NORMAL) to make sure you find hidden folders (or files).
—Cesar M.

Do you have comments on this tip? Let us know.

Related information from SearchDomino.com:

  • LotusScript Learning Guide
  • FAQ: LotusScript
  • Reference Center: LotusScript tips and resources

    Please let others know how useful this tip is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our tip contest and you could win a prize.

  • This was first published in July 2006

    Dig deeper on LotusScript

    0 comments

    Oldest 

    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to:

    SearchWinIT

    Search400

    • iSeries tutorials

      Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

    • V6R1 upgrade planning checklist

      When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

    • Connecting multiple iSeries systems through DDM

      Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

    SearchEnterpriseLinux

    SearchVirtualDataCentre.co.uk

    Close