PRB: No Trappable Error on Shell to Nonexistent .BAT File (118644)
The information in this article applies to:
- Microsoft Visual Basic Standard Edition for Windows 2.0
- Microsoft Visual Basic Standard Edition for Windows 3.0
- Microsoft Visual Basic Professional Edition for Windows 2.0
- Microsoft Visual Basic Professional Edition for Windows 3.0
- Microsoft Visual Basic Standard Edition for Windows 1.0
This article was previously published under Q118644 SYMPTOMS
When you execute the Shell command to run a batch (.BAT) file, no error
message is generated if the batch file specified does not exist.
CAUSE
This is by design and is the same behavior you see when using the Windows
API WinExec. "Shelling" to a batch program generates a handle to the
command processor, which runs the batch program. This handle is returned to
Visual Basic. When the command processor determines that there is no such
batch file, it is a process independent from Visual Basic and the error is
therefore not reported to Visual Basic.
Steps to Reproduce the Problem- Start Visual Basic; or, if Visual Basic is already running, choose New
Project (ALT + F, N) from the File menu. Form1 is created by default.
- Add a text box (Text1) to Form1 and type "Bogus.Bat" (without the
quotes) in the Text property.
- Add the following code to the Form_Click event for Form1:
Sub Form_Click()
Dim X As Integer
X = Shell((Text1.Text))
Print X, Err
'returns a valid process handle in x and err = 0
End Sub
- From the Run menu, choose Start (ALT + R, S), or press the F5 key to run
the program. Click the form.
The program attempts to run a nonexistent batch file and generates an error
outside the control of Visual Basic. If you enter a .EXE filename that does
not exist, Visual Basic correctly generates an error 53, "File Not Found".
Workaround
Replace the code from the above example with the following code:
Sub Form_Click()
Dim X As Integer
If Len(Dir((Text1.Text))) > 0 Then
X = Shell((Text1.Text))
Print X, Err
'returns a valid process handle in x and err = 0
Else
MsgBox "No such file exists."
End If
End Sub
Modification Type: | Major | Last Reviewed: | 12/9/2003 |
---|
Keywords: | kbprb KB118644 |
---|
|