Extracting filename from path

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Given a directory path string, such as "D:\documents\file1.txt", how do I
extract the filename part?
 
Charles,

Paste the following function in a standard module:

Function Extract_file_name(vFile As String) As String
pos = 0
For i = Len(vFile) To 1 Step -1
If Mid(vFile, i, 1) = "\" Then
pos = i
Exit For
End If
Next
Extract_file_name = Right(vFile, Len(vFile) - pos)
End Function

You can then call the function from anywhere in your project, passing
the directory path string as argument:
Extract_file_name("D:\documents\file1.txt")
and it will return the file name.

HTH,
Nikos
 
Charles,

Dir("D:\documents\file1.txt",vbNormal)

or

Dim sFile As String
sFile = "D:\documents\file1.txt"
Right(sFile, len(sfile)-InstrRev(sfile, "\",-1))

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Back
Top