Is Folder Exists

  • Thread starter Thread starter JCH
  • Start date Start date
J

JCH

I am trying to determine how to run a script that checks
to see if a particular folder exists. If it doesn't, then
I need to create it (MkDir), but if it does, then I skip
and move onto the next code segment...

Example:
If C:\My_Folder Exists Then
'Do Nothing
Else
'Folder does not exist
MkDir C:\My_Folder
End If

Any ideas?
Cheers,
JCH
 
Hi JCH,

The simple answer is to try to create the folder and trap the error if it
already exists:

On Error Resume Next
MkDir "C:\test"
On Error GoTo 0

This is more difficult if you want to build the whole subtree of folders if
they don't exist. If you know the root folder will exist, then this should
work fine.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
JCH,

The simplest way would be to call MkDir and ignore the error that
arises if the folder already exists. E.g.,


On Error Resume Next
MkDir "C:\Test"
On Error GoTo 0


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Hi JCH

Or this one

Sub MakeDirectory()
Dim Dirname As String
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dirname = "C:\MyDir2"
If Not fs.FolderExists(Dirname) Then
fs.CreateFolder Dirname
Else
'do nothing
End If
End Sub
 

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

Back
Top