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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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