HOWTO: Determine If a File Exists by Using DIR$

Last reviewed: March 20, 1997
Article ID: Q112674
The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Enterprise Edition, 16-bit and 32-bit, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SUMMARY

Visual Basic does not have any built-in functions that tell if a file exists or not. This article demonstrates how to find out if a file exists or not by using a Visual Basic program.

MORE INFORMATION

There are two different methods you can use to determine if a file exists:

  • Use the DIR/DIR$ command to determine if a file exists. This method is shown in the example below. The example performs a DIR on the filename and checks the return value. If the return value is "" (nothing), then the file doesn't exist.
  • Use the OPEN statement to open a file for input and an error trap. This method can run into problems when dealing with SHARE in Windows. If the file does not exist, a trappable error occurs. The only problem with this method is if the file that is being opened with the OPEN statement is in use, you will generate a sharing violation, which is a system level error that is not trappable from Visual Basic.

Step-by-Step Example

This example show how to check for the existence of a file by using the DIR$ function.

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a CommandButton (Command1) to Form1.

  3. Place the following code in the Command1_Click Event:

          Sub Command1_Click()
             Dim TheFile as String
             Dim Results as String
       
             TheFile = "C:\AUTOEXEC.BAT"
             Results = Dir$(TheFile)
       
             If Results  = "" Then
                MsgBox "File Doesn't Exist!"
             Else
                MsgBox "File does Exist!"
             End If
          End Sub
    
    

  4. Run the program, and click the Command1 button.

  5. Stop the program and make the TheFile variable point to a file that doesn't exist.

  6. Run the program again, and click the Command1 button.

Error in Visual Basic Help File Code

Please note that there is an error in the sample that is provided with the Visual Basic 3.0 Help file (NOTE: This only applies to Visual Basic 3.0, the samples provided with Visual Basic 4.0 and 5.0 are correct). The first line of the following If block is incorrect:

      If GetAttr(Path + DirName) And ATTR_DIRECTORY = ATTR_DIRECTORY Then
         If (Count Mod 10) = 0 Then
            ReDim Preserve D(Count + 10)  ' Resize the array.
         End If
         Count = Count + 1  ' Increment counter.
         D(Count) = DirName
      End If

Here's the correct version (only difference is the first line):

      If GetAttr(path + DirName) = ATTR_DIRECTORY Then
         If (Count Mod 10) = 0 Then
            ReDim Preserve D(Count + 10) ' Resize the array.
         End If
         Count = Count + 1 ' Increment counter.
         D(Count) = DirName
      End If

REFERENCES

For more information, see the Programmer's Reference, File Manipulation and the DIR/DIR$ commands.


Additional query words: existence
Keywords : kbprg PrgOther vb4all vb4win vb5all vb5howto kbhowto
Version : 3.0 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto


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: March 20, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.