Check if a directory exist and create a directory

raindrop3

Registered User.
Local time
Today, 23:13
Joined
Sep 6, 2001
Messages
98
Hello,

How can I check if a directory exist? I.e: I have a directory c:\database\photos\0. I want to check if this directory exist and then create a directory, called c:\database\photos\1.

Can VBA create a directory?

Thanks in advantage for helping me. It's becoming a serious problem for me.

Albert
 
To find out if a directory exists:

If Dir(strDirectoryPath)<>"" Then
True
Else: False
End if

To create a directory:

MkDir strDirectoryPath

Therefore this should do what you want:

Public Sub CheckDir(strDirectoryPath As String)

If Dir(strDirectoryPath)<>"" Then
MkDir strDirectoryPath
End if

End Sub

Ed Metcalfe.
 
Damn, not quite right. I'll try again:

Public Sub CheckDir(strDirectoryPath As String)

If Dir(strDirectoryPath)="" Then
MkDir strDirectoryPath
End if

End Sub

Ed Metcalfe
 
Thanks, ed. You pointed me on the right direction. I hope i can figure it out myself now!

Thanks again.
 
are you sure about that to use this, why don't you use c++??
see you later.
smile.gif
 
I can't use c because the application i'am workin at, is completely access-based...
 
And seems as this is an Access/VBA forum I thought the answer would answer itself really!

Ed Metcalfe.
 
FYI

If Dir(strDirectoryPath)="" then MkDir strDirectoryPath
Did not work properly for me in Access 2007, even if the directory existed, it still returned ""

However,
If Dir(strDirectoryPath,vbDirectory)="" then MkDir strDirectoryPath
worked properly.
 
This does not work for me!!!!
I'm using access 2003 (That is office computer no control on it)
I added this simple piece of code:

Code:
If dir("C:\Text Files", vbDirectory) <> "" Then
  MsgBox "dir worked"
End If

No matter this path exist or not this code results in an error and says "Compile Error: Expected Function or variable"

what is wrong?
 
you can try something like:

Code:
intPath = Len(Dir(strDirectoryPath,vbDirectory))
If intPath = 0 Then
   MkDir strDirectoryPath
End if

Cheers
 
This is compiler error, not run-time, it does not makes any difference where you put it?
 
It depends on where and how the whole code looks like

This in a Standard Module:

Code:
Function foo()
If Dir("d:\temp", vbDirectory) <> "" Then
    MsgBox "dir worked"
End If
End Function

Works.

JR
 
FYI

If Dir(strDirectoryPath)="" then MkDir strDirectoryPath
Did not work properly for me in Access 2007, even if the directory existed, it still returned ""

However,
If Dir(strDirectoryPath,vbDirectory)="" then MkDir strDirectoryPath
worked properly.

that is goooooooood and fix my problem
 
that is goooooooood and fix my problem

It works fine, but you can just add one level with this command.
So if you want to add two level (sub directorie) then you have to execute mkdir two times.

Success
 
This function works for me :-

Public Function CheckDir(strDirectoryPath As String) As Boolean
Dim IsDir As Boolean
If Len(Dir(strDirectoryPath, vbDirectory)) = 0 Then
IsDir = False
Else
IsDir = True
End If
CheckDir = IsDir
End Function
 

Users who are viewing this thread

Back
Top Bottom