Determine if directory exists, if not create it

kckay

Board Regular
Joined
Nov 8, 2010
Messages
134
How do I determine if a directory exists in VBA? How do I create a directory if it does not?

I woould like to check the status of a local directory. If it exists, I will move along to the next process. If it does not, I would like to create the directory on the C:\ drive and then move along to the next process.

Thank you.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Say the directory is c:\local
Code:
if Dir("c:\local",vbDirectory) ="" then
  'directory doesn't exist
else
  'directory does exist
end if
 
Upvote 0
Here are two approches:

Code:
Sub MakeMyFolder()
Dim fsoFSO
Set fsoFSO = CreateObject("Scripting.FileSystemObject")
If fsoFSO.FolderExists("C:\_Development\Deleteme") Then
    MsgBox "found it"
Else
    fsoFSO.CreateFolder ("C:\_Development\Deleteme")
    MsgBox "Done"
End If
End Sub
or....
Code:
Sub MakeMyFolder()
If Dir("C:\_Development\Deleteme", vbDirectory) = "" Then
    MkDir Path:="C:\_Development\Deleteme"
    MsgBox "Done"
Else
    MsgBox "found it"
End If
End Sub

Is that something you can work with?
 
Upvote 0
Beautiful! Works like a charm. Tested with and without numerous times.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,221,528
Messages
6,160,343
Members
451,638
Latest member
MyFlower

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top