[Sub] CreateDirectories - Approve Or Improve Please (1 Viewer)

BlueIshDan

☠
Local time
Yesterday, 23:11
Joined
May 15, 2014
Messages
1,122
sorry that was bugged

Code:
Public Sub CreateDirectoriesV2(ByVal address As String)
    
    Dim fs As New FileSystemObject
    Dim i As Integer: i = 1
    Dim str As String
    
    If Not fs.FolderExists(address) Then
        
        Do
            i = InStr(i + 1, address, "\")
            
            If i = 0 Then: Exit Do
            
            If Not fs.FolderExists(Mid(address, 1, i)) Then fs.CreateFolder Mid(address, 1, i)
        Loop
        
    End If
    
End Sub

Do you know about the Dir() function as well?

Kind of, but where do you think it applies here?
 

vbaInet

AWF VIP
Local time
Today, 02:11
Joined
Jan 22, 2010
Messages
26,374
Hmmm... no conditions in your loop.

Kind of, but where do you think it applies here?
Just making you aware of an alternative for checking if a file or folder exists, and it can use pattern matching too, whereas the FSO one doesn't do pattern matching.
 

BlueIshDan

☠
Local time
Yesterday, 23:11
Joined
May 15, 2014
Messages
1,122
V3! Hahaha
Code:
Public Sub CreateDirectoriesV3(ByVal address As String)
    
    Dim fs As New FileSystemObject
    Dim var_split As Variant
    Dim path As String
    
    If Not fs.FolderExists(address) Then
        
        For Each var_split In Split(address, "\")
        
            path = path & var_split & "\"
            If Not fs.FolderExists(path) Then: fs.CreateFolder path
            
        Next
        
    End If
    
End Sub

I'm also not so sure on how to use Dir for these things.
 

vbaInet

AWF VIP
Local time
Today, 02:11
Joined
Jan 22, 2010
Messages
26,374
Look it up in the help files in the VBA editor. It's easy to understand.

Better! Be careful so you don't go trying to create the root ;)
 

BlueIshDan

☠
Local time
Yesterday, 23:11
Joined
May 15, 2014
Messages
1,122
Thanks again for the guidance!
Just came back for the function, thought I'd give a thanks. =]
 

Users who are viewing this thread

Top Bottom