Extract filename from full path


I have a variable which contains the full file path for a file, example below:
C:\Documents and Settings\leecett\My Documents\output.txt

I want to extract the filename (without the file extension).

Note, the filename may contain other full stops / periods (e.g. output.one.txt), but will always have a file extension of .txt.

So I have a variable:
strFilePath = C:\Documents and Settings\leecett\My Documents\output.one.txt
I want a variable:
strFileName = output.one

Thanks leecett
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Assuming that the file name/path is in cell A1, and down the column, put this on cell B1 and copy down:


Check this link it should help. I would use the  instrev() function.


A little bit of code:

    strFilePath = "C:\Documents and Settings\leecett\My Documents\output.one.txt"
    strFileName = Right(strFilePath, Len(strFilePath) - InStrRev(strFilePath, "\"))
    strFileName = Replace(strFileName, "\", "")
    strFileName = Replace(strFileName, ".txt", "", 1, -1, vbTextCompare)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Try like this:

Dim strpath As String
Dim FileName As String
strpath = "\\blah\blah2\blah3\test.xls"
FileName = Mid(strpath, InStrRev(strpath, "\") + 1)
MsgBox (Left(FileName, Len(FileName) - 4))

Open in new window

MSmaxImplementation ConsultantCommented:
also try:

Dim temp As String, strFileName As String

temp = split(strFilePath, "\", vbBinaryCompare)
strFileName = Ubound(temp())
MSmaxImplementation ConsultantCommented:
now with missing comma

Dim temp As String, strFileName As String

temp = split(strFilePath, "\", ,vbBinaryCompare)
strFileName = Ubound(temp())
leecettAuthor Commented:
Thanks to you all for your advice and solution, I have developed the following:

Sub findfilename()
  Dim strFilePath As String
  Dim strFileName As String
  strFilePath = "C:\Documents and Settings\leecett\My Documents\output.one.txt"""
  strFileName = Mid(strFilePath, (InStrRev(strFilePath, "\")) + 1, (InStrRev(strFilePath, ".") - (InStrRev(strFilePath, "\") + 1)))
End Sub

Many thanks

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.