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
Search For:
Recent Posts
- HostPipe Clients Hitting the Local and National Press
- Scott Redding in Local Media | Motorsport PR and Website Design | Gloucestershire
- New website for sports media consultant launched
- Creating your Twitter Background, we've updated ours!
- The joys of previous versions of Internet Explorer
- The Twits | How not to use Social Media on your Website
- Cirencester Arena Website Goes Live
- The Web Design Process - Handling Your Budget
- Extracting the filename from a file path with a single line
- Web Design and the Credit Crunch in Stroud, Gloucestershire


Bookmark with: