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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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