|
|
|
|
|
|
Multiple Apps
|
Add trailing path separator (slash or colon) as necessary to folder path
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2003
|
Submitted by:
|
xCav8r
|
Description:
|
When given a folder path, it looks at the last character in the string to determine if a trailing path separator (slash or colon) should be added. If one is missing, it's added. If one is already there, the path remains unchanged.
|
Discussion:
|
If you ever have a need to concatenate (stick together) folder or file paths, then you're probably not unfamiliar with the unintended paths created as a result of a missing trailing path separator. This function was my simple answer to that headache. When used as a matter of habit, it reduces unnecessary error handling.
This function will work in any application on Macintosh or Windows. The demonstration is in Microsoft Word.
|
Code:
|
instructions for use
|
Option Explicit
Private Const mcstrPathWithSeparator As String = "C:\temp\"
Private Const mcstrPathWithoutSeparator As String = "C:"
Private Const mcstrPathSeparator As String = "\"
Private Sub TestMyTrailingSeparator()
Debug.Print FixTrailingSeparator(mcstrPathWithSeparator, mcstrPathSeparator)
Debug.Print FixTrailingSeparator(mcstrPathWithoutSeparator, mcstrPathSeparator)
End Sub
Public Function FixTrailingSeparator(Path As String, _
Optional PathSeparator As String = "\") As String
Select Case Right(Path, 1)
Case PathSeparator
FixTrailingSeparator = Path
Case Else
FixTrailingSeparator = Path & PathSeparator
End Select
End Function
|
How to use:
|
- Open your document.
- Press Alt + F11 to open the Visual Basic Editor (VBE).
- Insert a standard module. (Insert -> Module)
- Paste the code in the window to the right. (F7 to view; CTRL + V to paste)
- Save the file.
|
Test the code:
|
- From the VBE, make sure the Immediate Window is visible (CTRL + G)
- With your cursor anywhere in the module, press F5. (If your cursor is in the TestFixTrailingSeparator sub procedure, then you won't have to do the next step.)
- Select TestFixTrailingSeparator from the dialog box that appears, and then click Run.
|
Sample File:
|
AddTrailingPathSeparatorToFolderPath.zip 10.42KB
|
Approved by mdmackillop
|
This entry has been viewed 71 times.
|
|