Verify a directory exists

  • Thread starter Thread starter MacroMan
  • Start date Start date
M

MacroMan

I hope someone can help me with this. I am using VBA in
Excel XP with Windows 2000.

I am working with a legacy program in which the user
supplies a network path (e.g. \\col-gs1\subdir\temp\).

I cannot change the program other than to add a subroutine
that will verify that the user-supplied path is valid and
actually exists. This path is also typically empty, so I
can't look for any particular file either.

I know there are better ways to accomplish getting a path
from a user, but I must work within boss's constraints.

Your example code would be most appreciated. Thanks in
advance.
 
You can use this function

Function DirectoryExist(sstr As String)
'Tom Oglivy
Dim lngAttr As Long
DirectoryExist = False
If Dir(sstr, vbDirectory) <> "" Then
lngAttr = GetAttr(sstr)
If lngAttr And vbDirectory Then _
DirectoryExist = True
End If
End Function

and use it like this

Dim Dirname As String
Dirname = "C:\MyDir"
If Not DirectoryExist(Dirname) Then
'.........................
Else
'............................
End If
 
You can force the directory to exist using a windows API
call:

Private Declare Function MakeSureDirectoryPathExists
Lib "imagehlp.dll" (ByVal lpPath As String) As Long

The lpPath argument can be a mapped network volume or its
UNC name, it should end with \.

If you do not want to force it to exist,

Private Declare Function PathFileExists Lib "shlwapi.dll"
Alias "PathFileExistsA" (ByVal pszPath As String) As Long

for a directory, pszPath must end with \.
 
Read help for FolderExists Method.

Regards Eric Bentzen
(remove nospam to reply)
 
Back
Top