Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Split values

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
mustish1
Asked:
mustish1
  • 2
1 Solution
 
Ryan ChongCommented:
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
 
Ryan ChongCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now