Check if a directory exist and create a directory (1 Viewer)

raindrop3

Registered User.
Local time
Today, 07:23
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
 

ed_metcalfe

New member
Local time
Today, 07:23
Joined
Nov 12, 2001
Messages
7
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.
 

ed_metcalfe

New member
Local time
Today, 07:23
Joined
Nov 12, 2001
Messages
7
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
 

raindrop3

Registered User.
Local time
Today, 07:23
Joined
Sep 6, 2001
Messages
98
Thanks, ed. You pointed me on the right direction. I hope i can figure it out myself now!

Thanks again.
 
H

hutmannetje

Guest
are you sure about that to use this, why don't you use c++??
see you later.
 

raindrop3

Registered User.
Local time
Today, 07:23
Joined
Sep 6, 2001
Messages
98
I can't use c because the application i'am workin at, is completely access-based...
 

ed_metcalfe

New member
Local time
Today, 07:23
Joined
Nov 12, 2001
Messages
7
And seems as this is an Access/VBA forum I thought the answer would answer itself really!

Ed Metcalfe.
 

selahlynch

Registered User.
Local time
Today, 10:23
Joined
Jan 3, 2010
Messages
63
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.
 

Anishtain4

Registered User.
Local time
Yesterday, 23:23
Joined
Apr 13, 2011
Messages
21
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?
 

mane_uk

Registered User.
Local time
Today, 07:23
Joined
Feb 14, 2011
Messages
45
you can try something like:

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

Cheers
 

Anishtain4

Registered User.
Local time
Yesterday, 23:23
Joined
Apr 13, 2011
Messages
21
This is compiler error, not run-time, it does not makes any difference where you put it?
 

JANR

Registered User.
Local time
Today, 08:23
Joined
Jan 21, 2009
Messages
1,623
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
 

bee55

Registered User.
Local time
Yesterday, 23:23
Joined
Oct 27, 2011
Messages
50
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
 

foosterhuis

New member
Local time
Today, 08:23
Joined
May 4, 2012
Messages
1
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
 

AngusGodden

New member
Local time
Today, 07:23
Joined
Jul 29, 2014
Messages
1
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

Top Bottom