Skip Navigation Links.
Create Directory Path - Recursevly creates a path to any depth.
Language(s):Visual Basic 6.0
Category(s):Recursion, Utilities

Recursevly creates a directory path to any depth.

' 1) Create a new project. Form1 will be created by default.
' 2) Add a command button and a text box to the form. 
' 3) Paste the following code into the declarations section of Form1:

' --- Begin code for Form1 ---

'Create Directory Path
'
'Author: Jon Vote 
'        Idioma Software Inc. 
'        www.idioma-software.com 
'
'Date:   10/02
'
'(c) 2002 Idioma Software Inc. 

'May be freely used in code. 
'
'No other use is permitted. 
'
'User uses at own risk.

Option Explicit

Private Declare Function GetFileAttributes _
  Lib "kernel32" Alias "GetFileAttributesA" _
     (ByVal lpFileName As String) As Long

Private Sub Command1_Click()
  CreateDirectoryPath Text1.Text
End Sub

Private Sub Form_Load()
  
  Me.Caption = "Create Directory Path - www.skycoder.com"
  Text1.Text = "c:\temp\temp2\temp3\temp4\temp5\temp6\"
  Command1.Caption = "Create"
  
End Sub

'Recursevly creates a directory path
Private Sub CreateDirectoryPath(ByVal strCreateThis As String)

  Dim strParentPath As String
  
  strParentPath = ParentPath(strCreateThis)
  
  'Recursevly call ourselves back if
  'until the parent directory exists
  If GetFileAttributes(strParentPath) < 0 Then
     CreateDirectoryPath strParentPath
  End If
  
  'We get here when the parent directory exists
  MkDir strCreateThis
  
End Sub

Private Function ParentPath(ByVal strPath As String) As String

  Dim s As Integer
  If Right$(strPath, 1) = "\" Then
     strPath = Left$(strPath, Len(strPath) - 1)
  End If
  s = InStrRev(strPath, "\")
  If s = 0 Then
      strPath = "\"
  Else
      ParentPath = Left$(strPath, s)
  End If
  
End Function


' --- End code for Module1 ---
This article has been viewed 4344 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.