Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Extracting the directory name
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00520254
Message ID:
00521012
Views:
19
>>From ASP, I would like to extract the directory name. How can I achieve that?
>
>I found this but it returns the full path. If there is a simpliest way to retrieve just the name of the current folder, that would be ok.
>
>
>Set oFSO=CreateObject("Scripting.FileSystemObject")
>oFSO.GetFolder(Server.MapPath("."))
>
You don't need to use the FileSystemObject to do this. There is a member of the ServerVariables collection in the Request object that stores the complete path and file name of the page you are on. Using this and a some string manipulation you can come up with the directory name of the current page being viewed:
' Get the full path for the page including the page name
strFullPath = Request.ServerVariables("PATH_TRANSLATED")

' Determine the character position of the last backslash in the path
intBackslashPos = InStrRev(strFullPath, "\")

' Strip off the file name and the last backslash
strPathOnly = Mid(strFullPath, 1, intBackslashPos-1)

' Determine the character position of the last backslash in the path
intBackslashPos = InStrRev(strPathOnly, "\")

' Now strip off everything else up to the last remaining 
' backslash leaving just the directory name
strDirectory = Mid(strPathOnly, intBackslashPos+1)
The InStrRev() function finds the first location of a character string starting at the end of the searched string and going right to left. I broke out the backslash locating code in the above example to show how I used it. In practice however, I would embed the InStrRev() functions in the Mid() functions like this:
' Get the full path for the page including the page name
strFullPath = Request.ServerVariables("PATH_TRANSLATED")

' Strip off the file name and the last backslash
strPathOnly = Mid(strFullPath,1,InStrRev(strFullPath,"\")-1)

' Now strip off everything else up to the last remaining
' backslash leaving just the directory name
strDirectory = Mid(strPathOnly,InStrRev(strPathOnly,"\")+1)
You could actually do the whole thing in one line of code, but it would be a little difficult to read and you would also end up making 4 references to the ServerVariables collection which is a performance hit. While the ServerVariables collection is full of little goodies like this, every reference to it involves a procedure call outside of the ASP processor to IIS, so it should only be used when necessary. If you're going to reference a particular ServerVariables item more than once in your code, assign the contents to a variable to avoid these out-of-process calls.

HTH
"It is an important and popular fact that things are not always what they seem. For instance, on the planet Earth, man had always assumed that he was more intelligent than dolphins because he had achieved so much -- the wheel, New York, wars and so on -- whilst all the dolphins had ever done was muck about in the water having a good time. But conversely, the dolphins had always believed that they were far more intelligent than man -- for precisely the same reasons." - Douglas Adams
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform