Script Paradigm

パスを連結

ファイルパスを連結します。
例えばフォルダパス+フォルダ名や、フォルダ+ファイル名といった結果を返します。

Option Explicit
MsgBox fnConCastPath("C:\abc","test.txt")
MsgBox fnConCastPath("C:\abc\","test.txt")
MsgBox fnConCastPath("C:\abc","\test.txt")
MsgBox fnConCastPath("C:\abc\","\test.txt")
'パスの連結を行う。連結部分の「\」を適切に処理する。
Function fnConCastPath(ByVal strPath1, ByVal strPath2)
    Dim strWallChar
    Dim strTempChar
    
    '★区切り文字を設定
    If Instr(strPath1 & strPath2,"/") > 0 Then
        strWallChar = "/"
    Else
        strWallChar = "\"
    End If
    
    '★パス1の末尾に区切り文字がない場合は付加
    If strPath1 <> "" Then
        strTempChar = Right(strPath1, 1)
        If strTempChar <> "/" And strTempChar <> "\" Then
            strPath1 = strPath1 & strWallChar
        End If
    End If
    
    '★パス2の先頭に区切り文字がある場合は消去
    If strPath2 <> "" Then
        strTempChar = Left(strPath2, 1)
        If strTempChar = "/" Or strTempChar = "\" Then
            strPath2 = Right(strPath2, Len(strPath2) - 1)
        End If
    End If
    
    fnConCastPath = strPath1 & strPath2
End Function
Copyright © 2006 Hikijishi All Rights Reserved.
[] [wsh][0.00177383422851562]