Script (File i/o)

Hi,

Does anyone know if it is possible to test the result of a file i/o operation in a script?

E.g. Open “FILE.TXT” for Input As #1, what if the file does not exist ?

Johan de Waardt
ABN AMRO Bank


Listserv Archives (BOB member since 2002-06-25)

Hi:

You can always do a Dir(“FileName”) to see if the file exists. However, the input operation will create a new file if one does not exist. You may be interested in our ReportScript manual, check out www.bobooks.com for details.

Robert


Schmidt Interactive Software, Inc.

Listserv Archives (BOB member since 2002-06-25)

In a message dated 98-07-07 08:07:09 EDT, you write:

Hi,

Does anyone know if it is possible to test the result of a file i/o operation in a script?

E.g. Open “FILE.TXT” for Input As #1, what if the file does not exist ?

Johan de Waardt
ABN AMRO Bank

Johan:

Attempting to open a file for INPUT that does not exist will generate an error. What you can do is capture the error and handle it as you wish. Following is sample code that turns off the default error handler and attempts to open a file for input. If the file does not exist, the supplied error handling code will allow you to write code to react to the situation. If the file opens correctly, the default error handler is restored and code continues.

Sub Main

Dim FileName$

FileName$ = “C:\AUTOEXEC.BAT”
’ Try this first, then try with C:\AUTOEXEC.BAD to test a non-existing file

’ First Check to ensure that FileName is a valid file

On Error Goto NoFile ’ provide alternate error handling

Open FileName$ For Input As 1 ’ attempt to open file
On Error Resume Next ’ if success, restore default error
handling
Close 1 ’ close file (it was only a test,
afterall)

’ continue with normal code, including opening file for input ’ note: this can be used to detect any file, whether the ’ ultimate intent is to read, write, or append

Exit Sub

’ Exception code here
ShutDown: ’ label for error handling
Close
Exit Sub

NoFile: ’ error handler for missing file

MsgBox “File " & FileName$ & " Missing!” Resume ShutDown ’ resume at a label
End Sub

Someone else suggested:

You can always do a Dir(“FileName”) to see if the file exists. However, the
input operation will create a new file if one does not exist.

As I said at the beginning, opening a file in INPUT mode does not create the file if it does not exist. You get a script error. This is what should happen. You can’t read from a nonexisting file, nor should your program expect that to be the situation. A missing input file is an exception that should be handled.

Opening a file in OUTPUT or APPEND mode will create a file if it does not exist.

However, Dir(“FileName”) will work; you have to test the results to see if the command returned any values. Code to do that would look like:

Dirname$ = Dir(“MyFile.txt”)

If (Dirname$ = “” ) Then
MsgBox “Missing Input File!”
Else
’ Open file for input and process here
End If

Regards,
Dave Rathbun
Integra Solutions
www.islink.com


Listserv Archives (BOB member since 2002-06-25)