VB and VBA Users Source Code: Joining or Concatenation two paths together
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Joining or Concatenation two paths together
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, August 24, 2005
Hits:
2303
Category:
Files/Directories/IO
Article:
Below is a simple routine which joins two paths together to form a fully qualified path (eg "C:\Program Files" and "My Application" becomes "C:\Program Files\My Application\"). A demonstration routine can be found at the bottom of this post. 'Purpose : Appends or concatenates two paths together. 'Inputs : sPath1 The base directory or path. ' sPath2 The directory (or file) to append to the base directory. ' [bAppendTrailingSlash] True to append a trailing backslash (if sPath2 is a file name then use false). 'Outputs : Returns the paths combined, with trailing slash. 'Author : Andrew Baker 'Date : 25/03/2005 'Notes : Public Function PathConcatenate(sPath1 As String, sPath2 As String, Optional bAppendTrailingSlash As Boolean = True) As String Dim sPathSepChar As String 'Determine if we are using forward or back slashes If InStr(sPath1 & sPath2, "/") > 0 Then sPathSepChar = "/" Else sPathSepChar = "\" End If If Len(sPath1) = 0 Then PathConcatenate = sPath2 ElseIf Len(sPath2) = 0 Then PathConcatenate = sPath1 Else If Right$(sPath1, 1) <> sPathSepChar Then sPath1 = sPath1 & sPathSepChar End If If Left$(sPath2, 1) = sPathSepChar Then sPath2 = Mid(sPath2, 2) End If 'Return the path PathConcatenate = sPath1 & sPath2 End If If bAppendTrailingSlash Then 'Append trailing slash If Right$(PathConcatenate, 1) <> sPathSepChar Then PathConcatenate = PathConcatenate & sPathSepChar End If End If End Function 'Demonstration routine Sub Test() Dim sPath1 As String, sPath2 As String sPath1 = "C:\" sPath2 = "MyFolder\SubDir1" Debug.Print "Appending folders: " & PathConcatenate(sPath1, sPath2) sPath1 = "http://www.vbusers.com" sPath2 = "code/codetoc.asp" Debug.Print "Appending web paths: " & PathConcatenate(sPath1, sPath2) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder