String Manipulation Extract File Name From Path

I have a string in a variable called MyPath.

MyPath = "c:\program files\microsoft\office\word.exe"

How do I strip that string so it only returns the file name.

BUT, the file name can be of any size, e.g. "excel.exe" or "ThisIsATestLetterForSomeThing.doc" etc etc

Thanks for reading
LVL 1
narmi2Asked:
Who is Participating?
 
RonaldBiemansConnect With a Mentor Commented:
or the .net way

 Dim filename As String = Path.GetFileName(MyPath)
0
 
LacutahConnect With a Mentor Commented:
MyPath.Substring(MyPath.LastIndexOf("\") + 1)
0
 
RacinRanConnect With a Mentor Commented:
Dim MyFile as String

MyFile =  MyPath.Substring(MyPath.LastIndexOf("\") + 1)

Racin
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
RacinRanCommented:
I should'a refreshed ... you beat me lacutah.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You need:

    Imports System.IO

outside your class for RonaldBiemans code...or you must fully qualify the call with the namespace:

    Dim filename As String = System.IO.Path.GetFileName(MyPath)

~IM
0
 
RonaldBiemansCommented:
to extend on this

Dim filename As String = Path.GetPathRoot(MyPath)  ' will return word.exe
Dim Root as string = Path.GetPathRoot(MyPath) ' will return c:\
Dim directory As String = Path.GetDirectoryName(MyPath) ' will return c:\program files\microsoft\office\
0
 
RonaldBiemansCommented:
Thanks Idle_Mind :-)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use th FileInfo class:

    Dim MyPath As String = "c:\program files\microsoft\office\word.exe"
    Dim fi As New System.IO.FileInfo(MyPath)
    Dim msg As String = "Directory: " & fi.DirectoryName & vbCrLf & _
        "File: " & fi.Name
    MsgBox(msg)

~IM
0
 
narmi2Author Commented:
Thanks :)
0
 
loyalhighCommented:
Here's another way to do it with text or label

Me.Path.Text = "C:\myFile\myPicture.jpg"
        'declare an Array to splite the path
        Dim spArray() As String
        spArray = Split(Me.Path.Text, "\")
        'Now spArray hold the first letter which C
        'because the \ after :
        Me.lblResult.Text = spArray(0)
        'Now reverse that to be the last dash \
        spArray.Reverse(spArray)
        Me.lblResult1.Text = spArray(0)

        '<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>
        'Or as simple as you can do this
        'Me.lblResult.Text = _
        'Path.Text.Substring(Path.Text.LastIndexOf("\"))
0
 
loyalhighCommented:
i am sorry i forgot to comment : after C Drive
here's the complete Code & comments

Me.Path.Text = "C:\myFile\myPicture.jpg"
        'declare an Array to splite the path
        Dim spArray() As String
        spArray = Split(Me.Path.Text, "\")
        'Now spArray hold the first letter which C:
        'because the \ after :
        Me.lblResult.Text = spArray(0)
        'Now reverse that to be the last dash \
        spArray.Reverse(spArray)
        Me.lblResult1.Text = spArray(0)

        '<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>
        'Or as simple as you can do this
        'Me.lblResult.Text = _
        'Path.Text.Substring(Path.Text.LastIndexOf("\"))
0
All Courses

From novice to tech pro — start learning today.