Solved

Split values

Posted on 2007-03-21
2
187 Views
Last Modified: 2010-04-30
Hi: Can any one please tell me how to i split file name and folder from the variable
filetext.text = "C:\Documents and Settings\khamu443\My Documents\test.txt"

so it would be
filename="test.txt"
folderaddress="C:\Documents and Settings\khamu443\My Documents\"

Thanks.
0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 18764345
try:

Public Function getDir(filePath As String, Optional separator As String = "\") As String
    Dim tmp As String, I As Long
    tmp = StrReverse(filePath)
    I = InStr(1, tmp, separator)
    If I > 0 Then
        tmp = StrReverse(Mid$(tmp, I))
    Else
        tmp = ""
    End If
    getDir = tmp
End Function

Public Function getFileName(filePath As String, Optional separator As String = "\", Optional removeExtension As Boolean = False) As String
    Dim tmp As String
    tmp = Mid$(filePath, InStrRev(filePath, separator) + 1)
    If tmp = filePath Then
        getFileName = ""
    Else
        If removeExtension Then
            If InStrRev(tmp, ".") > 0 Then
                getFileName = Left$(tmp, InStrRev(tmp, ".") - 1)
            Else
                getFileName = tmp
            End If
        Else
            getFileName = tmp
        End If
    End If
End Function
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 18764393
Here is an example:

filetext.Text = "C:\Documents and Settings\khamu443\My Documents\test.txt"
    filename = getFileName(filetext.Text)
    folderaddress = getDir(filetext.Text)
   
    MsgBox "filename: " & filename & vbCrLf & "folderaddress: " & folderaddress


hope this helps
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question