Creating a folder

D

dan

Sub test()
' I want to creat a folder if that folder doesn't exist in the path.
' The following statement is not supported. What is the right way?, Please.

If Not Application.exist("I:\Mybackup\") Then MkDir ("I:\Mybackup\")
End Sub
 
J

JW

One way:
Sub addFolder()
Dim pth As String
pth = "I:\Mybackup\"
If Dir(pth, vbDirectory) = "" Then
MkDir pth
Else
MsgBox "exists"
End If
End Sub

Another way:
On Error Resume Next
MkDir "I:\Mybackup\"
On Error GoTo0

I personally prefer the first way.
 
G

Guest

Use Dir

Sub test()

' I want to creat a folder if that folder doesn't exist in the path.
' The following statement is not supported. What is the right way?, Please.

If Dir("C:\Mybackup\") = "" Then
MkDir ("C:\temp\Mybackup\")
End If
End Sub
 
D

dan

Thank you very much, JW

JW said:
One way:
Sub addFolder()
Dim pth As String
pth = "I:\Mybackup\"
If Dir(pth, vbDirectory) = "" Then
MkDir pth
Else
MsgBox "exists"
End If
End Sub

Another way:
On Error Resume Next
MkDir "I:\Mybackup\"
On Error GoTo0

I personally prefer the first way.
 
D

Dave Peterson

Maybe you can just ignore the error if the folder already exists:

on error resume next
mkdir "I:\mybackup"
 

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

Similar Threads


Top