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.

Download this free guide

Download: IT Certifications 101

Inside this exclusive essential guide, our independent experts break down which IT certifications are worth your time and effort, and how to get started obtaining them to further your career— including specific certifications that any cloud or desktop pro should seriously consider.

By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

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.

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.

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.

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.

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.

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.

0 comments

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy