Solved

Split values

Posted on 2007-03-21
2
185 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
  • 2
2 Comments
 
LVL 51

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 51

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

Industry Leaders: 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!

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

749 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