Is there a command to see if dir exists?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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!
 
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.
 
Hmmm... Didn't know anything about any overhead for a FileSystemObject. I
will reconsider my direction on this. Thanks again!
 
Back
Top