The File NameOnly function

This function accepts one argument (a path with filename) and returns only the filename. In other words, it strips out the path.

Private Function FileNameOnly(pname) As String ' Returns the filename from a path/filename string

Dim i As Integer, length As Integer, temp As String Dim Cnt As Integer ' Count the path separator characters Cnt = 0

If Mid(pname, i, 1) = Application.PathSeparator Then

FileNameOnly = Split(pname, Application.PathSeparator, Cnt) End Function

If the argument is c:\excel files\2007\backup\budget .xls, the function returns the string budget.xls.

The FileNameOnly function works with any path and filename (even if the file does not exist). If the file exists, the following function is a simpler way to strip off the path and return only the filename.

Private Function FileNameOnly2(pname) As String

FileNameOnly2 = Dir(pname) End Function

0 0

Post a comment