VB and VBA Users Source Code: Separate File name and File Path from String (Fun with Split & Join)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Separate File name and File Path from String (Fun with Split & Join)
By:
Jeffrey J Reddy
Email (spam proof):
Email the originator of this post
Date:
Wednesday, May 05, 2004
Hits:
1613
Category:
Visual Basic General
Article:
Create a form and add the following controls: TextBox - txtPath Label - lblPath Label - lblFile Button - cmdParsePath Copy and Paste Following Code Private Sub cmdParsePath_Click() Dim strPath As String Dim strFileName As String strPath = txtPath.Text SeparatePathAndFile strPath, strFileName lblPath.Caption = strPath lblFile.Caption = strFileName End Sub Private Sub SeparatePathAndFile(ByRef io_strPath As String, ByRef o_strFileName As String) 'io_strPath - Input/output parameter containing the entire path with file name ' - Will Return the path only 'o_strFileName - Output parameter that will contain the name of the File Dim strPath() As String Dim lngIndex As Long strPath() = Split(io_strPath, "\") 'Put the Parts of our path into an array lngIndex = UBound(strPath) o_strFileName = strPath(lngIndex) 'Get the File Name from our array strPath(lngIndex) = "" 'Remove the File Name from our array io_strPath = Join(strPath, "\") 'Rebuild our path from our array End Sub NOTES: Code assumes path being passed is valid Path with File Name.
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder