VB and VBA Users Source Code: Create a directory structure
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Create a directory structure
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, August 23, 2000
Hits:
979
Category:
Files/Directories/IO
Article:
The routines below can be used to create a directory or folder structure. The MakeDir routine checks from the root directory downwards to determine which directories already exist and creates all any new directories required. Option Explicit 'Purpose : Makes a directory/path (inc. any the required parent directories) 'Inputs : sPath The directory to create 'Outputs : Returns True if succeeded in creating directory 'Author : Andrewb 'Date : 17/08/2000 'Notes : 'Revisions : Function MakeDir(ByVal sPath As String) As Boolean Dim asDirs() As String, sDir As String Dim lStartPos As Long, lThisDir As Long, lNumDirs As Long If DirExists(sPath) = False Then 'Ignore errors when creating paths that already exist 'or UNC paths, then check if the path exists at the end On Error Resume Next If Right$(sPath, 1) = "\" Then sPath = Left$(sPath, Len(sPath) - 1) End If asDirs = Split(sPath, "\") lNumDirs = UBound(asDirs) sDir = "" For lThisDir = 0 To lNumDirs sDir = sDir & asDirs(lThisDir) & "\" If Len(asDirs(lThisDir)) Then If DirExists(sDir) = False Then MkDir sDir End If End If Next 'Check if the directory exists MakeDir = DirExists(sPath) Else MakeDir = True End If End Function 'Purpose : Check if a Path Exists 'Inputs : sPath The path to check 'Outputs : Returns True if the path exists, False if it doesn't 'Author : Andrewb 'Date : 17/08/2000 'Notes : 'Revisions : Function DirExists(ByVal sPath As String) As Boolean If sPath <> ".." And sPath <> "." And sPath <> "\" And Len(sPath) Then If Right$(sPath, 1) <> "\" Then sPath = sPath & "\" End If On Error Resume Next DirExists = (GetAttr(sPath) And vbDirectory) > 0 On Error GoTo 0 End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder