skip to content

Extracting the filename from a file path with a single line

There are frequent times when a full file path is extracted from a database or passed to your function and you need the file name. This handy Classic ASP one liner can be used to pull out the filename part of the path in a single hit.

sFilePath = “C:/websites/mywebsite/images/my-logo.jpg”
sFileName = Right(sFilePath, Len(sFilePath) - InStrRev(sFilePath,"/"))

Remember that although the InStrRev function searches the string from the end, it still returns the location of the search string counting from the start.

You could wrap this in a function with the path as a parameter and it could easily be extended to require the directory separator as an additional parameter to deal with different path formats.

Tags: asp | code | filepath | filename

This entry was posted on 26 January 2009 at 14:46 and is filed under ASP Code Snippet. You can leave a response here.

Leave a Reply