How to Add a backslash if the string doesn’t have one already (in Two ways)
Posted on January 4, 2009
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 | 'File/Folder Handling - How to Add a backslash if the string doesn't have one already (in Two ways) Option Explicit 'Adds a backslash to the end of a string to create the correct syntax for a path. 'If the source path already has a trailing backslash, no backslash will be added. 'Non-API Method: Public Function AddBackslash(ByVal sFolder As String) As String If Len(sFolder) > 0 Then If Right$(sFolder, 1) <> "\" Then AddBackslash = sFolder + "\" Else AddBackslash = sFolder End If End If End Function 'API Method 1: Private Declare Function PathAddBackslash Lib "shlwapi.dll" Alias "PathAddBackslashA" _ (ByVal pszPath As String) As Long Public Function AddBackslashUsingAPI(ByVal sFolder As String) As String sFolder = sFolder & String(1, 0) PathAddBackslash sFolder AddBackslashUsingAPI = sFolder End Function 'API Method 2: Global Const gstrSEP_DIR$ = "\" ' Directory separator character Global Const gstrSEP_AMPERSAND$ = "@" Global Const gstrSEP_REGKEY$ = "\" ' Registration key separator character. Global Const gstrSEP_DRIVE$ = ":" ' Driver separater character, e.g., C:\ Global Const gstrSEP_DIRALT$ = "/" ' Alternate directory separator character Global Const gstrSEP_EXT$ = "." ' Filename extension separator character Global Const gstrSEP_PROGID = "." Global Const gstrSEP_FILE$ = "|" ' Use the character for delimiting filename lists because it is not a valid character in a filename. Global Const gstrSEP_LIST = "|" Global Const gstrSEP_URL$ = "://" ' Separator that follows HPPT in URL address Global Const gstrSEP_URLDIR$ = "/" ' Separator for dividing directories in URL addresses. Public Function AddDirSep(ByVal strPathName As String) If Right(Trim(strPathName), Len(gstrSEP_URLDIR)) <> gstrSEP_URLDIR And _ Right(Trim(strPathName), Len(gstrSEP_DIR)) <> gstrSEP_DIR Then strPathName = RTrim$(strPathName) & gstrSEP_DIR End If AddDirSep = strPathName End Function |