VB and VBA Users Source Code: Check if a directory exists using API calls
[
Home
|
Contents
|
Search
|
Reply
|
Previous
| Next ]
VB/VBA Source Code
Check if a directory exists using API calls
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, October 02, 2000
Hits:
979
Category:
Files/Directories/IO
Article:
The following routine demonstrates how to check if a directory/folder exists using API calls: Private Const MAX_PATH = 260 Private Const INVALID_HANDLE_VALUE = -1 Private Const FILE_ATTRIBUTE_DIRECTORY = &H10 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 directory/folder Exists 'Inputs : sFolder The name of the folder to test 'Outputs : Returns True if folder exists 'Author : Andrew Baker 'Date : 02/10/2000 15:11 'Notes : 'Revisions : Function FolderExists(ByVal sFolder As String) As Boolean Dim hFile As Long Dim WFD As WIN32_FIND_DATA If Len(sFolder) > 0 Then On Error Resume Next 'Remove training slash before verifying If Right$(sFolder, 1) = "\" Then sFolder = Left$(sFolder, Len(sFolder) - 1) End If 'call the API pasing the folder hFile = FindFirstFile(sFolder, WFD) 'if a valid file handle was returned, and the directory attribute is set 'the folder exists FolderExists = (hFile <> INVALID_HANDLE_VALUE) And (WFD.FileAttributes And FILE_ATTRIBUTE_DIRECTORY) 'Close Handle Call FindClose(hFile) On Error GoTo 0 End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder