Extracting the filename from a file path with a single line
26 January 2009 at 14:46
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,"/"))
InStrRev Function
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.
Posted in ASP Code Snippet | No Comments


Social Network
Recent Posts
Categories
Archives
RSS Feed