Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding FileName in FilePath

General Information

Finding FileName in FilePath

by  MikeC14081972  Posted    (Edited  )
A question I get asked a lot is how to extract a filename from a full filepath.

Whilst you can use InStr and loop through the filepath I have found it much easier to start at the other end of the path and work backwards

i.e. Passing in a FullPath of "C:\MyDocuments\Test.txt" to the below function will extract a filename of Test.txt

Code:
Function FileName (FullPath as string)

Dim FileName as string
Dim Pos as Integer

Pos = InStrRev (FullPath,"\",,vbTextCompare)
FileName = Mid(FullPath,Pos +1)

Debug.Print FileName

Exit Function

The code starts at the end of the string and Pos in the position of the first "\" moving backwards.

Using the Mid function you can then extract the filename by starting at position pos+1
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top