PRB: No Trappable Error on Shell to Nonexistent .BAT File

Last reviewed: June 21, 1995
Article ID: Q118644
The information in this article applies to:
  • Microsoft Visual Basic for Windows, versions 2.0 and 3.0, Standard and Professional Editions
  • Microsoft Visual Basic Programming System for Windows, version 1.0

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.

MORE INFORMATION

The following is from the Help topic "Shell":

   The Visual Basic Shell command runs an executable program. If the
   Shell() function successfully executes the named program, it returns
   the instance handle of the started program. If the Shell() function
   can't start the named program, an error occurs.

However, if you use Shell to run a batch file that does not exist, a modal dialog box inclusing the following text is generated:

     Cannot find file.
     Check to make sure the path and filename are correct.

In this case, Visual Basic continues to run, no error message is generated, and the return value from Shell() has a legitimate Windows instance handle.

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

  1. 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.

  2. Add a text box (Text1) to Form1 and type "Bogus.Bat" (without the quotes) in the Text property.

  3. 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
    
    

  4. 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


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode kbprb
KBSubCategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.