Visual Basic > Applications-VBA
Separate File name and File Path from String (Fun with Split & Join)
Separate File name and File Path from String (Fun with Split & Join) 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.
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows