experchange > vb.* > vb.general.discussion

BillyJ (02-27-20, 05:24 PM)
When I Gurgle the Internet I find all kinds of FolderExists (FileExists
etc) routines but not easily find this one. I find routines using Dir
(Dir is dangerous so I avoid it), Open File, FindFirstFile or
FindFirstFileW and the unusable FSO.

Why ? Something I need to know about "Shlwapi"

Looking for the cheapest and fastest.

' Returns TRUE if the path is a valid directory, or FALSE otherwise.
Private Declare Function PathIsDirectory Lib "Shlwapi" Alias
"PathIsDirectoryW" (ByVal lpszPath As Long) As Boolean

Public Function FolderExists(sPath As String) As Boolean

' Returns True if the folder name is valid
FolderExists = PathIsDirectory(StrPtr(sPath))

End Function
ObiWan (02-27-20, 05:34 PM)
:: On Thu, 27 Feb 2020 07:24:09 -0800
:: (microsoft.public.vb.general.discussion)
:: <r38muq$1q7j$1>
:: BillyJ <BillyJ> wrote:

> ' Returns TRUE if the path is a valid directory, or FALSE otherwise.
> Private Declare Function PathIsDirectory Lib "Shlwapi" Alias
> "PathIsDirectoryW" (ByVal lpszPath As Long) As Boolean


Pure VB6, no API declaration/call, no ext dependencies

Function FolderExists(ByVal sPath As String) As Boolean
Dim bRes As Boolean

On Error Resume Next
If (GetAttr(sPath) And vbDirectory) = vbDirectory Then
bRes = True
End If
FolderExists = bRes
End Function
Mayayana (02-27-20, 06:36 PM)
"ObiWan" <obiwan> wrote

Function FolderExists(ByVal sPath As String) As Boolean
Dim bRes As Boolean

On Error Resume Next
If (GetAttr(sPath) And vbDirectory) = vbDirectory Then
bRes = True
End If
FolderExists = bRes
End Function

That's what I use, in a terse version:

On Error Resume Next
FolderExists = (GetAttr(sPath) And vbDirectory) = vbDirectory
Err.Clear

As I recall, there was an exhaustive discussion of this
many years ago and GetAttr came out on top.
ObiWan (02-28-20, 10:00 AM)
:: On Thu, 27 Feb 2020 11:36:20 -0500
:: (microsoft.public.vb.general.discussion)
:: <r38r7v$t6s$1>
:: "Mayayana" <mayayana> wrote:

> That's what I use, in a terse version:
> On Error Resume Next
> FolderExists = (GetAttr(sPath) And vbDirectory) = vbDirectory
> Err.Clear
> As I recall, there was an exhaustive discussion of this
> many years ago and GetAttr came out on top.


For sure it's much more compact, you may make it even more compact
On Error Resume Next
FolderExists = CBool(GetAttr(sPath) And vbDirectory)

:D
BillyJ (02-28-20, 09:05 PM)
Am I correct that any of this is not for long path name or Unicode ?

So ...
Arnoutdv (03-02-20, 12:11 PM)
Op vrijdag 28 februari 2020 20:05:53 UTC+1 schreef BillyJ:
> Am I correct that any of this is not for long path name or Unicode ?
> So ...


Try this:

'---------------
Private Declare Function GetFileAttributesW Lib "kernel32" (ByVal lpFileName As Long) As Long

Public Function PathFileExists(ByVal sPath As String) As Boolean
PathFileExists = (GetFileAttributesW(StrPtr(sPath)) <> -1)
End Function
'---------------

Arnout
BillyJ (03-02-20, 07:04 PM)
Better.

I thought there was a GetFileAttributesW but could not find it in use
on-line.

I think your code needs to be more specific.

Thanks to your sample I found Bonnie West with interesting code:



Some of which is:

Private Const DRIVE_NO_ROOT_DIR As Long = 1
Private Const ERROR_SHARING_VIOLATION As Long = 32

Private Declare Function GetDriveTypeW Lib "kernel32" ( _
ByVal lpRootPathName As Long _
) As Long

Private Declare Function GetFileAttributesW Lib "kernel32" ( _
ByVal lpFileName As Long _
) As Long

Select Case (GetFileAttributesW(StrPtr(sFileName)) And vbDirectory) = 0
Case True: FileExists = True
Case Else: FileExists = (Err.LastDllError = ERROR_SHARING_VIOLATION)
End Select
'
'························ ····· v12.0 ························· ·······
'
'This one-liner form of the above does the LastDllError check everytime.
'
'FileExists = ((GetFileAttributesW(StrPtr(sFileName)) And vbDirectory) _
' = 0) Or (Err.LastDllError = ERROR_SHARING_VIOLATION)
End Function

'================================================= ========================

Public Function DirExists(ByRef sPath As String) As Boolean
DirExists = Abs(GetFileAttributesW(StrPtr(sPath))) And vbDirectory
End Function

Public Function DriveExists(ByRef sDrive As String) As Boolean
DriveExists = GetDriveTypeW(StrPtr(sDrive)) <> DRIVE_NO_ROOT_DIR
End Function

Public Function GetVolumeLabel(ByRef sDrive As String) As String
GetVolumeLabel = Dir$(sDrive, vbVolume)
End Function

' ================================================== ==============

Arnoutdv wrote:
[..]
Similar Threads