Visual Basic File API Routines
FindFirstFile: An API 'FileExists' Routine
     
Posted:   Wednesday June 18, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

 

FindFirstFile: Changing File and/or Folder Attributes Recursively
FindFirstFile: Fast Directory File Count

FindFirstFile: Extract Filename from a Full Path
FindFirstFile: Performance Comparison - FSO vs. API
FindFirstFile: Comparison of FindFirstFile and SearchTreeForFile
FindFirstFile: Save a Recursive Search of All Drives to Disk
FindFirstFile: Save a Recursive Search of Specified Drives to Disk
GetFileVersionInfo: File Search and File Property Info
GetLogicalDriveStrings: An API 'DriveExists' Routine
FindFirstFile: An API 'FileExists' Routine
FindFirstFile: An API 'FolderExists' Routine
PathFileExists: A Local/Network File/Folder/Drive Exists Routine
     
 Prerequisites
None.

Some things in life are inevitable, and to the VB developer one of these is finding themselves scouring the VB documentation for the needed but non-existent 'FileExists()' routine. But while missing from the VB arsenal, via newsgroups they'll find it in several flavours, all created as wrapper functions developed by others who have preceded their quest.

Most of these methods take one of two approaches ... the hack method .. using the file Open command on the would-be file and trapping the resulting error if the Open statement fails, and the more-common Dir() method, which attempts to do execute the Dir command against the file. This routine, in true VBnet fashion, says pshaw to those and takes the API approach, with the corresponding performance increase. And if you already have API file routines already in place, chances are you have already laid the declarations to make the API FileExists function.

As this is a simple wrapper, I'll forego the form design and present the routine as a Public bas module function, with a command button to test it. So just start a new project, add a bas module, form and command button, and the code below.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const INVALID_HANDLE_VALUE = -1
Public Const MAX_PATH As Long = 260

Public Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" _
   Alias "FindFirstFileA" _
  (ByVal lpFileName As String, _
   lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindClose Lib "kernel32" _
  (ByVal hFindFile As Long) As Long


Public Function FileExists(sSource As String) As Boolean

   Dim WFD As WIN32_FIND_DATA
   Dim hFile As Long
   
   hFile = FindFirstFile(sSource, WFD)
   FileExists = hFile <> INVALID_HANDLE_VALUE
   
   Call FindClose(hFile)
   
End Function
 Form Code
Drop a command button onto a form and add the following:

Option Explicit

Private Sub Command1_Click()

   MsgBox FileExists("c:\windows\system\comctl32.dll")

End Sub
 Comments
Couldn't get much simpler, eh? Just pass the path and filename, and get True or False back.

Could you use instead a one-liner (FileExists = FindFirstFile(sSource, WFD) <> INVALID_HANDLE_VALUE) ? Technically, yes, you will get the correct result.  But according to the MSDN, you need to use the method detailed above in order to close the file handle opened by the call.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter