create folder or folder/subfolder if not exist

S

Song

some users only have ..\elac folder but no subfolder ..\elac\PhoneBook.
Others even do not have ..\elac folder at all. How to create \elac\PhoneBook
or ..\PhoneBook subfolder if they do not exist? Here is my code:

Private Sub cmdPhoneBook_Click()
Dim strSource As String
Dim strTarget As String

strSource = "S:\Apps\PhoneBook\Phone.pdf"
strTarget = Environ("AppData") & "\elac\PhoneBook\Phone.pdf"

If Len(Dir(strTarget)) > 0 Then
If FileDateTime(strSource) > FileDateTime(strTarget) Then
FileCopy strSource, strTarget
End If
Else
FileCopy strSource, strTarget
End If

Application.FollowHyperlink strTarget

End Sub
 
S

Song

If found it:

'check to see if folder exist, if not create one.
If Len(Dir(Environ("AppData") & "\elac\PhoneBook", vbDirectory)) = 0 Then
MkDir Environ("AppData") & "\elac\PhoneBook"
End If
 
D

Douglas J. Steele

Just note that if Environ("AppData") & "\elac\" doesn't already exist, your
code will fail. You should probably use:

If Len(Dir(Environ("AppData") & "\elac", vbDirectory)) = 0 Then
MkDir Environ("AppData") & "\elac"
End If
If Len(Dir(Environ("AppData") & "\elac\PhoneBook", vbDirectory)) = 0 Then
MkDir Environ("AppData") & "\elac\PhoneBook"
End If
 
Top