Home > Domino Tips > Developer > LotusScript > Finding files and directories with LotusScript
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

LOTUSSCRIPT

Finding files and directories with LotusScript


Chuck Connell
07.18.2006
Rating: -3.69- (out of 5)


Lotus Notes, Domino, Workplace and WebSphere tips and advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


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.

    Rate this Tip
    To rate tips, you must be a member of SearchDomino.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


    RELATED CONTENT
    LotusScript
    Reading a binary field in an Oracle database with LotusScript
    LotusScript equivalent of @Picklist for Lotus Notes
    Launch large attachments within an email from a Notes database
    How to find files on a hard drive or mapped network with LotusScript
    Update the ACL from the Roles view with LotusScript
    LotusScript agent moves tagged spam email to junk mail folder
    Create a personalized greeting for Lotus Notes database users
    Fail-safe rich-text validation using LotusScript
    How to export Lotus Notes views to a Microsoft Excel database
    Create file system labels for Microsoft Excel and Word mail merges

    LotusScript
    Reading a binary field in an Oracle database with LotusScript
    LotusScript equivalent of @Picklist for Lotus Notes
    Launch large attachments within an email from a Notes database
    How to find files on a hard drive or mapped network with LotusScript
    Update the ACL from the Roles view with LotusScript
    LotusScript agent moves tagged spam email to junk mail folder
    Set a value in a field existing in another Lotus Notes database
    Create an automatic scheduled view export in Excel
    Top 10 LotusScript tips
    Create a personalized greeting for Lotus Notes database users

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

  • HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




    All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts