|
![]() |
![]() ![]() |
![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic File API Routines FindFirstFile: An API 'FolderExists' Routine |
|
Posted: | Wednesday October 20, 1999 |
Updated: | Monday December 26, 2011 |
Applies to: | VB4-32, VB5, VB6 |
Developed with: | VB6, Windows NT4 |
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. |
|
![]() The method calls FindFirstFile passing the folder of interest. If the result of the call, the Find handle, is not invalid, and the returned WIN32_FIND_DATA dwAttributes member contains the FILE_ATTRIBUTE_DIRECTORY flag, then the folder exists. This method also works against UNC drives.
If a remote machine's drive is mapped to a share, e.g. drive C is mapped
to the share named 'drivec', then the function will return true if a
subfolder under C is specified, e.g. \\remotepc\drivec\windows\ or \\remotepc\drivec\windows\system32\drivers\etc\.
Remember that C:\, D:\ etc. are not folders but root drives, so this code will return false. To test for root drives, use the DriveExists method under Related Items above. |
BAS Module Code |
None. |
|
Form Code |
![]() |
Drop a text box, label and command button onto a form, and add the following: |
|
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. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const MAX_PATH As Long = 260 Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10 Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private 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 Private Declare Function FindFirstFile Lib "kernel32" _ Alias "FindFirstFileA" _ (ByVal lpFileName As String, _ lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" _ (ByVal hFindFile As Long) As Long Private Sub Command1_Click() 'pass the text to the FolderExists 'routine, returning the result to a label. If Len(Text1.Text) > 0 Then Label1.Caption = FolderExists(Text1.Text) End If End Sub Private Function FolderExists(sFolder As String) As Boolean Dim hFile As Long Dim WFD As WIN32_FIND_DATA 'remove training slash before verifying sFolder = UnQualifyPath(sFolder) '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.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) 'clean up Call FindClose(hFile) End Function Private Function UnQualifyPath(ByVal sFolder As String) As String 'trim and remove any trailing slash sFolder = Trim$(sFolder) If Right$(sFolder, 1) = "\" Then UnQualifyPath = Left$(sFolder, Len(sFolder) - 1) Else UnQualifyPath = sFolder End If End Function |
Comments |
This function will return the correct result regardless of any additional attributes assigned to the directory. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |