VB and VBA Users Source Code: Checking if a File Exists MK II
[
Home
|
Contents
|
Search
|
Reply
|
Previous
| Next ]
VB/VBA Source Code
Checking if a File Exists MK II
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, October 02, 2000
Hits:
623
Category:
Files/Directories/IO
Article:
An alternative API version of checking if a file exists using is: Private Const MAX_PATH = 260 Private Const INVALID_HANDLE_VALUE = -1 Private Type FILETIME LowDateTime As Long HighDateTime As Long End Type Private Type WIN32_FIND_DATA FileAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long Reserved0 As Long Reserved1 As Long cFileName As String * MAX_PATH cAlternate As String * 14 End Type Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long 'Purpose : Determines if a File Exists 'Inputs : sFilePathName The file and path name of the file to test 'Outputs : Returns True if the file exists 'Author : Andrew Baker 'Date : 02/10/2000 15:11 'Notes : 'Revisions : Public Function FileExists(sFilePathName As String) As Boolean Dim WFD As WIN32_FIND_DATA Dim hFile As Long If Len(sFilePathName) > 0 Then If Right$(sFilePathName, 1) <> "." And Right$(sFilePathName, 2) <> ".." Then hFile = FindFirstFile(sFilePathName, WFD) FileExists = (hFile <> INVALID_HANDLE_VALUE) Call FindClose(hFile) End If End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder