Path: Combine

Combine

Combines two or more strings into a path.



 Public Function Combine(
	  ByRef Path1 As String,
	  ByRef Path2 As String,
	  ParamArray Paths ( ) As Variant ) As String

Parameters

Path1
[ByRef] String. The first path to combine.
Path2
[ByRef] String. The second path to combine.
Paths
[ByRef] Variant. Zero or more additional paths to be combined.

Return Values

String -  The combined paths.

Remarks

Path1 should be an absolute path (for example, "d:\archives" or "\\archives\public"). If Path2 or a string in Paths is also an absolute path, the combine operation discards all previously combined paths and resets to that absolute path.

Zero-length strings are omitted from the combined path.

If Path1 is not a drive reference (that is, "C:" or "D:") and does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to Path1 before concatenation.

If Path2 does not include a root (for example, if Path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If Path2 includes a root, Path2 is returned.

The parameters are not parsed if they have white space. Therefore, if Path2 includes white space (for example, " c:\ "), the Combine method appends Path2 to Path1 instead of returning only Path2.

Not all invalid characters for directory and file names are interpreted as unacceptable by the Combine method, because you can use these characters for search wildcard characters. For example, while Path.Combine("c:\", "*.txt") might be invalid if you were to create a file from it, it is valid as a search string. It is therefore successfully interpreted by the Combine method.

Exceptions

ExceptionCondition
ArgumentException Path1, Path2, or a string element in Paths contains one or more of the invalid characters defined in GetInvalidPathChars.

Examples

The following code example demonstrates using the Combine method.

Public Sub Main()
    Const Path1 As String = "c:\Temp"
    Const Path2 As String = "SubDir\File.txt"
    Const Path3 As String = "c:\Temp.txt"
    Const Path4 As String = "c:^*&)(_=#'\^&#2.*(.txt"
    Const Path5 As String = ""
    Const Path6 As String = vbNullString
    
    CombinePaths Path1, Path2
    CombinePaths Path1, Path3
    CombinePaths Path3, Path2
    CombinePaths Path4, Path2
    CombinePaths Path5, Path2
    CombinePaths Path6, Path2
End Sub

Private Sub CombinePaths(ByVal P1 As String, ByVal P2 As String)
    Dim Combination As String
    
    Combination = Path.Combine(P1, P2)
    
    Debug.Print CorString.Format("When you combine '{0}' and '{1}', the result is: {2}'{3}'", P1, P2, vbCrLf, Combination)
    Debug.Print
    Exit Sub
End Sub

' This example code produces the following output.
'
'    When you combine 'c:\Temp' and 'SubDir\File.txt', the result is:
'    'c:\Temp\SubDir\File.txt'
'
'    When you combine 'c:\Temp' and 'c:\Temp.txt', the result is:
'    'c:\Temp.txt'
'
'    When you combine 'c:\Temp.txt' and 'SubDir\File.txt', the result is:
'    'c:\Temp.txt\SubDir\File.txt'
'
'    When you combine 'c:^*&)(_=#'\^&#2.*(.txt' and 'SubDir\File.txt', the result is:
'    'c:^*&)(_=#'\^&#2.*(.txt\SubDir\File.txt'
'
'    When you combine '' and 'SubDir\File.txt', the result is:
'    'SubDir\File.txt'
'
'    When you combine '' and 'SubDir\File.txt', the result is:
'    'SubDir\File.txt'

See Also

Project CorLib Overview

Class Path Overview

CombineArray