Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the 2nd last folder in a folder path?

Status
Not open for further replies.

tayorolls

MIS
May 8, 2007
42
US
Hi,

I am trying to accomplish this in JScript and ASP. I can get the last folder in the path, but not the 2nd last one. For instance in the example below, I can "Sales Agreement", but I am not able to get "Standard Form Agreements and Templates". How can I get that?

Thanks.

My browser output is this:

Code:
Folder: \\monet\deptweb\Contracts\Standard Form Agreements and Templates\Sales Agreement
folderPath1: monet/deptweb/Contracts/Standard Form Agreements and Templates/Sales Agreement
folderPath2: monet,deptweb,Contracts,Standard Form Agreements and Templates,Sales Agreement
folderDisplay1: Sales Agreement
folderDisplay2: undefined

This is the code that outputs the above output:

Code:
var folderString1 = folderspec;
		var folderPath1 = folderString1.substr(2, folderString1.length - 2);
		Response.Write ("folderPath1: " + folderPath1 + "<br/>");		
		var folderPath2 = folderPath1.split("/");
		Response.Write ("folderPath2: " + folderPath2 + "<br/>");		
		var folderPathLen1 = folderPath2.length;
		var folderDisplay1 = folderPath2[folderPathLen1-1];
		var folderDisplay2 = folderPath2[folderPathLen1];
        Response.Write ("folderDisplay1: " + folderDisplay1 + "<br/>");		
		Response.Write ("folderDisplay2: " + folderDisplay2 + "<br/>");
 
If you use the Split() command to split the string on \ then you will get an array of all of the folder names. The item located at the UBound(arr) of the array is the last folder, UBound(arr)-1 is second to last array, etc.

Best MS KB Ever:
 
The FileSystemObject has a built-in function named GetParentFolderName so you could call that once to get the parent and then pass the result string back into a second call of the same function to get the "grandparent" folder path.

[tt]
Set fso = Server.CreateObject("Scripting.FileSystemObject")
parent = fso.GetParentFolderName(folderspec)
grandparent = fso.GetParentFolderName(parent)
Set fso = Nothing
[/tt]

From a code efficiency point of view, I would rather user Tarwn's approach because parsing the path string would be a lot faster and scale up a lot better than creating the FSO object... But if I already had the FSO object in use on the page for some other reason then I might say GetParentFolderName was easier.


To see more about GetFolderName:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top