by Bill Jones (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (5 Votes)
Creates all non-existing folders in a path. Local or network UNC path.
Inputs
CreateThisPath as string
Private Sub CreateDirectoryStruct(CreateThisPath As String)
'do initial check
Dim ret As Boolean, temp$, ComputerName As String, IntoItCount As Integer, x%, WakeString As String
Dim MadeIt As Integer
If Dir$(CreateThisPath, vbDirectory) <> "" Then Exit Sub
'is this a network path?
If Left$(CreateThisPath, 2) = "\\" Then ' this is a UNC NetworkPath
'must extract the machine name first, then get to the first folder
IntoItCount = 3
ComputerName = Mid$(CreateThisPath, IntoItCount, InStr(IntoItCount, CreateThisPath, "\") - IntoItCount)
IntoItCount = IntoItCount + Len(ComputerName) + 1
IntoItCount = InStr(IntoItCount, CreateThisPath, "\") + 1
'temp = Mid$(CreateThisPath, IntoItCount, x)
Else ' this is a regular path
IntoItCount = 4
End If
WakeString = Left$(CreateThisPath, IntoItCount - 1)
'start a loop through the CreateThisPath string
Do
x = InStr(IntoItCount, CreateThisPath, "\")
If x <> 0 Then
x = x - IntoItCount
temp = Mid$(CreateThisPath, IntoItCount, x)
Else
temp = Mid$(CreateThisPath, IntoItCount)
End If
IntoItCount = IntoItCount + Len(temp) + 1
temp = WakeString + temp
'Create a directory if it doesn't already exist
ret = (Dir$(temp, vbDirectory) <> "")
If Not ret Then
'ret& = CreateDirectory(temp, Security)
MkDir temp
End If
IntoItCount = IntoItCount 'track where we are in the string
WakeString = Left$(CreateThisPath, IntoItCount - 1)
Loop While WakeString <> CreateThisPath
End Sub