Is there a way to determine if a directory exists?
I have tried:
If Dir(strPath & IIf(Right(strPath, 1) = "", "", ""), vbDirectory) = "" Then
What I get when the code is executed is:
Run time error 52
Bad file name or number
Is there a way to determine if a directory exists?
I have tried:
If Dir(strPath & IIf(Right(strPath, 1) = "", "", ""), vbDirectory) = "" Then
What I get when the code is executed is:
Run time error 52
Bad file name or number

This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
Public Function DirectoryExists(Dir As String) As Boolean
Dim oDir As New Scripting.FileSystemObject
DirectoryExists = oDir.FolderExists(Dir)
End Function
found at http://www.freevbcode.com/ShowCode.asp?ID=29
--------------------------------------------------
Jack MacDonald
Vancouver, Canada
Here is another way to do it.
If Dir(strFoldername, vbDirectory) = "" Then
' folder does not exist
Regards
John
Thanks for your help, but what do I need to tick in references to make it work?
That is the problem I am having. Have another look at my first post.
Patt,
try adding a reference to
Windows Script Host Object Model which = windirsystem32wshom.ocx
That should then work a treat.
Stewart
Patt,
you could also try
Public Function IsPathValid(ByVal strUNCPath As String) As Boolean
'COMMENTS:
'
' - Determine whether the specified path exists.
Dim strFolder As String 'Buffer for current folder.
On Error GoTo ERRHandler
strFolder = CurDir()
ChDir strUNCPath
ChDir strFolder
IsPathValid = True
Exit Function
ERRHandler:
IsPathValid = False
ChDir strFolder
Exit Function
End Function 'IsPathValid()
The problem I am having is a compile error, User type not defined on the following line:
Dim oDir As New Scripting.FileSystemObject
<P ID="edit" class=small>(Edited by patt on 16-Jun-04 18:06. correction)</P>What does the ChDir command do? I know what this does now.
Thanks Stewart, that works well.
Sorry Pat
I didn't read carefully enough.
What happens if you just use:
If Dir(strPath, vbDirectory) = "" Then
In my testing, it does not seem necessary to add a "" to the end.
Regards
John
Don't be sorry, I do that as well, I read things in too much of a hurry sometimes and regret it later.
Your suggestion here is what I originally had and it keeps coming up with "Bad filename or number".
I thought originally it must have been a reference I had not declared.
Thanks for your post, they all help.
For those of us who remember DOS, there is an easy way. Every directory has a NUL component, so you merely have to look for NUL using your DIR function. I've modified your code below, the function will return NUL if the directory exists:
If Dir(strPath & IIf(Right(strPath, 1) = "", "", "") & "NUL") = "NUL" Then
Mark Liquorman
See my website for Tips & Downloads and for my Liquorman Utilities.
I have finally managed to produce the same error as you by putting something completely inappropriate in strpath.
Perhaps you might check what is in it before testing with dir, by putting msgbox(strpath).
I get the error if the path uses a full UNC path, but don't follow the computer name with a drive letter.
eg strpath = "Johncabdos" gives that error, but
strpath = "Johncdos" does not.
Regards
John
No, that does not work where the directory does not exist, your code thinks that the directory exists.
. I have the situation where I develop the system on my d: drive then copy it for a client who operated it on his c: drive. I have paths setup in a table which I use. If I don't change these from d:... to c:... I get the error.