CreateFolders

By | 2002-06-01

Make nested subfolders in a single method.

Original Author: Gerald Bryant

Inputs

sPath: Fully-qualified absolute or relative path you wish to create.
‘Example 1: \NetworkVolumeNetworkShareExistingDirNewDirNewSubdirNewSubDir
‘Example 2: C:Program Filesacdefghijklm opq s uvwxyz

Assumptions

Add reference “Microsoft Scripting Runtime” (scrrun.dll) available at http://www.microsoft.com/scripting or with VB6.

Side Effects

Number 70, Permission Denied error will occur is write access or directory create access is not allowed for the drive for that user.
‘Reference “Microsoft Scripting Runtime” (scrrun.dll) available at http://www.microsoft.com/scripting or with VB6.

API Declarations

Add reference “Microsoft Scripting Runtime” (scrrun.dll) available at http://www.microsoft.com/scripting or with VB6.

Code

Public Sub CreateFolders(ByVal sPath As String)
Dim oFileSystem As New Scripting.FileSystemObject
'or late-bind with:
'Dim oFileSystem As Object
'Set oFileSystem = CreateObject("Scripting.FileSystemObject")
On Error GoTo ErrorHandler
With oFileSystem
  ' Is this drive valid and ready?
  If .DriveExists(.GetDriveName(sPath)) Then
   ' Is this folder not yet valid?
   If Not .FolderExists(sPath) Then
    ' Recurse back in to this method until a parent folder is valid.
    CreateFolders .GetParentFolderName(sPath)
    ' Create only a nonexistant folder before exiting the method.
     .CreateFolder sPath
   End If
  End If
End With
Set oFileSystem = Nothing
ExitMethod:
Exit Sub
ErrorHandler:
App.LogEvent "CreateFolders Error in " & Err.Source & _
": Could not create " & sPath & ".", vbLogEventTypeInformation
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.