Skip to content

Instantly share code, notes, and snippets.

@timotewb
Last active April 18, 2020 09:22
Show Gist options
  • Save timotewb/334a7cf998a8a6c11471cfb0af463569 to your computer and use it in GitHub Desktop.
Save timotewb/334a7cf998a8a6c11471cfb0af463569 to your computer and use it in GitHub Desktop.
Private Function createDIR(dir As String) As Boolean
'--- Create Directory (recursivley)
' excel vba code to take string input as path and recursivley create
' folders if not exists. windows only.
'
' dir: full path to directory you want created (string)
'--- setup
createDIR = False
Dim dirList As Variant
Dim dirString As String
'--- check if network path
If Left(dir, Len(dir) - (Len(dir) - 2)) = "\\" Then
dir = Right(dir, Len(dir) - 2)
dirString = "\\"
End If
dirList = Split(dir, "\")
'--- craete each folder
For i = 0 To Application.CountA(dirList) - 1
If i = 0 Then
'--- skip the first drive/share
dirString = dirString & dirList(i)
Else
dirString = dirString & "\" & dirList(i)
With CreateObject("Scripting.FileSystemObject")
If Not .FolderExists(dirString) Then .CreateFolder dirString
End With
End If
Next i
'--- check directtory exists
With CreateObject("Scripting.FileSystemObject")
If .FolderExists(dirString) Then createDIR = True
End With
End Function
'--- example call from within sub
Sub callFunction()
MsgBox createDIR("\\ip_address\share01\test01\test02")
MsgBox createDIR("C:\temp\test01\test02")
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment