Is there a command to see if dir exists?

M

Melvis

Hi,

I have a database that installs another database. I need it to make the
directory, but only if it does not already exist. Is there a VBA command to
check for the existence of a folder? When I run mkdir on an existing folder I
get a permissions error. This is what I need to do: (pseudocode)

if "c:\matt" does not exist then
mkdir "c:\matt"
end if

Thanks in advance for any help!

~MATT
 
D

Dirk Goldgar

Melvis said:
Hi,

I have a database that installs another database. I need it to make
the directory, but only if it does not already exist. Is there a VBA
command to check for the existence of a folder? When I run mkdir on
an existing folder I get a permissions error. This is what I need to
do: (pseudocode)

if "c:\matt" does not exist then
mkdir "c:\matt"
end if

Thanks in advance for any help!

~MATT

How about

If Len(Dir("C:\Matt", vbDirectory)) = 0 Then
MkDir "C:\Matt"
End If
 
M

Melvis

Thanks a bunch Dirk!

I also found a way to do it that was more intuitive for me. The code looks
like this:

Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:\matt") Then
MsgBox "Folder already exists!"
Else
MkDir "c:\matt"
End If

Thanks again for your reply - it got me looking in the right place!
 
D

Dirk Goldgar

Melvis said:
Thanks a bunch Dirk!

I also found a way to do it that was more intuitive for me. The code
looks like this:

Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:\matt") Then
MsgBox "Folder already exists!"
Else
MkDir "c:\matt"
End If

I wouldn't incur the overhead of creating a FileSystemObject when
there's a native VBA function to accomplish the same end, not to mention
the concern that scripting may be disabled on some systems. But
whatever works for you.
Thanks again for your reply - it got me looking in the right place!

You're welcome.
 
M

Melvis

Hmmm... Didn't know anything about any overhead for a FileSystemObject. I
will reconsider my direction on this. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top