Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Files Directories Drives Code Examples

Create a directory structure

Create a directory structure 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 '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 '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