README.md

December 5, 2022 ยท View on GitHub

CreateFolders

Description

Make nested subfolders in a single method.

More Info

sPath: Fully-qualified absolute or relative path you wish to create.

'Example 1: \NetworkVolume\NetworkShare\ExistingDir\NewDir\NewSubdir\NewSubDir\

'Example 2: C:\Program Files\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z

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

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.

Submitted On
ByGerald Bryant
LevelUnknown
User Rating4.2 (162 globes from 39 users)
CompatibilityVB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script
CategoryVB function enhancement
WorldVisual Basic
Archive File

API Declarations

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

Source 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