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 > Applications VBA Code Examples

How to Parse Out a Path

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
How to Parse Out a Path The following class module will take a path such as 'C:\vb\projects\pathparse\cfileparse.cls' and parse out the different elements. Just assign the path to the FullPath property and then read the properties for Drive, Path, Filename, and Extension. Create a New class module and insert this code Option Explicit Private msFullPath As String Private msPath As String Private msDrive As String Private msFile As String Private msExt As String Public Property Get FullPath() As String FullPath = msFullPath End Property Public Property Let FullPath(sNewFullPath As String) Dim iPos As Integer Dim iCounter As Integer msFullPath = sNewFullPath msDrive = "" msPath = "" msFile = "" msExt = "" 'Get the Drive iPos = InStr(1, msFullPath, ":") If iPos > 0 Then 'Drive exists msDrive = Left$(msFullPath, iPos) End If If Right$(msFullPath, 1) = "/" Or _ Right$(msFullPath, 1) = "\" Or _ InStr(1, msFullPath, ".") = 0 Then 'No file specified in this path msFile = "" msExt = "" msPath = msFullPath 'Append a backslash if one does not exist If Right$(msPath, 1) <> "/" And _ Right$(msPath, 1) <> "\" Then msPath = msPath & "\" End If Else iPos = Len(msFullPath) 'Loop backwards through msFullPath _ 'until we have all of the filename For iCounter = iPos To 1 Step -1 If Mid$(msFullPath, iCounter, 1) = "/" _ Or Mid$(msFullPath, iCounter, 1) = "\" Then 'No more characters for the file Exit For Else msFile = Mid$(msFullPath, _ iCounter, 1) & msFile End If Next 'Parse out the path without the filename msPath = Left$(msFullPath, _ Len(msFullPath) - Len(msFile)) 'Get the extension iPos = InStr(1, msFile, ".") msExt = Right$(msFile, Len(msFile) - iPos) End If End Property Public Property Get Path() As String Path = msPath End Property Public Property Get Drive() As String Drive = msDrive End Property Public Property Get File() As String File = msFile End Property Public Property Get Extension() As String Extension = msExt End Property