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

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
If you approve of the way this currently works then please quick post: APPROVE
If you know of a way to improve this then PLEASE DO post your ideas! (PLEASE DO)

Hello, I would like to pass this one by the minds of the more experienced here at this forum! :)

Could you please read over and try to suggest improvements for efficiency and/or approve that it is good as it currently stands. :)

This Sub is made to create the directories for the address sent to it:

Starting from the end and working its way back, the script checks to see if the folders currently exist. Once found, it rubber bands forward and begins to create all non-existing folders.

Reference: Micosoft Scripting Runtime

Code:
Public Sub CreateDirectories(ByVal address As String)
    
    ' Verify that the address is not a drive.
    ' You can do this by checking that the length of the address
    ' is creater than 4
    ' C:\x\
    
    If Len(address & vbNullString) > 4 Then
        
        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 If
    
End Sub

Thanks,
BlueIshDan :)

Improvements:

Added declaration of i and j Variants for use of Option Explicit
Removed double check of folder existance, since it is checked already.
 
Last edited:

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
I would actually like to bump this. My first post on this forum haha, sorry!

I would like to know if this is a reasonable way of creating directories or if there is already a built in function that will build the path if non existent.

Again, sorry for the bump!
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
Give us an example of an address.
 

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
C:\test01\test02\test03\test04\test05\test06\test07\test08\test09\test10\

Or maybe a mapped drive W:\test01\test02\test03\test04\test05\test06\test07\test08\test09\test10\
Or maybe a server address \\servername\test01\test02\test03\test04\test05\test06\test07\test08\test09\test10\
 

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
Noticed that I didn't use Option explicit back then. So adding that will help I guess for the i, j variants. lol
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
Ok I can see some things that can be improved upon, but do you really want us to work on this code or do you want to know other ways of doing it instead of reinventing the wheel?
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
Remember MkDir from Shell, it makes the subdirectories. There's MkDir in VBA but that doesn't create missing subdirectories.
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
You can call Shell from VBA too. To invoke the Shell MkDir (not the VBA one) you do something like this:
Code:
Shell "cmd.exe /c mkdir " & Chr(34) & address & Chr(34)
 

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
Yes but the point of this sub is to be able to create all directories missing.
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
That's what I explained to you in post #10.

MkDir in VBA and MkDir in Shell are two different functions. MkDir in VBA doesn't create sub directories, but MkDir (Shell or Command Prompt's own MkDir) creates subdirectories. The names are the same, the libraries are different and one has more functionality than the other.

I'm calling the Shell's own MkDir.

Makes sense now?
 

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
Starting to realize I "reinvent the wheel" in a lot of scenarios...
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
By the way, with regards to your original code, I can advise two things:

1. Instead of checking each character use Instr() and change your start position for the next iteration
2. A second loop isn't required. If a "\" is found, perform the check and create the directory if necessary.
 

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
Starting to realize I "reinvent the wheel" in a lot of scenarios...
You'll be surprised how much is readily available. It's still a good exercise to have a go anyway.

I'm surprised that no one replied to your post especially as it was your first. :)
 

BlueIshDan

☠
Local time
Today, 00:37
Joined
May 15, 2014
Messages
1,122
By the way, with regards to your original code, I can advise two things:

1. Instead of checking each character use Instr() and change your start position for the next iteration
2. A second loop isn't required. If a "\" is found, perform the check and create the directory if necessary.

Right, like this?

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

vbaInet

AWF VIP
Local time
Today, 04:37
Joined
Jan 22, 2010
Messages
26,374
Yep, just like that. Just make sure the InStr() position is the correct length for the Mid()

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

Users who are viewing this thread

Top Bottom