Testing for Successful Open of a Text File During an Agent I/O Call

Author: Tripp W Black

Created: 03/06/2003 at 09:19 AM

 

Category:
Notes Developer Tips
Agents, LotusScript

When dealing with I/O operations; always use error-handling:

On Error Goto MyErrorhandler
Open "MyFile.txt" For Input As #1
Do Until EOF(1)
Line Input #1,tmp
Loop
exitthesub:
Close #1
Exit Sub
MyErrorhandler:
Msgbox "Error #" & cstr(err) & " - " & Error$,16,"Error!"
Resume exitthesub


But to test wheather a file actually exists before trying to open it: Use the DIR-command:

tmp = Dir$("C:\MyText.txt")
If the file exists, then the tmp-variable will containt the filename: "MyText.txt" and if the file doesn't exists then the variable will be an empty string: ""
Note: this last tip may also through an error #76 Path Not Found. In which case, simply add a On Error 76 Goto PathNotFoundHandler statement to the top of the code in the handler create the directory/file or set the original variable to another "good" path and Resume Next.

e.g.

On Error 76 Goto PathErrorHandler
...
...
Exit Sub
PathErrorHandler:
directory$="c:\"
Resume Next

previous page