Jump to content

Set current folder name as a variable?

Recommended Posts

Hi,A site I'm developing will have new folders added regularly. Each folder represents a category. I'd like to use the same template file in each new folder. To save editing the template with the category name each time, I'd like to create a variable which takes the category name from the folder name:myurl/category1/mytemplate.asp > variable = "category1"myurl/category2/mytemplate.asp > variable = "category2"myurl/category3/mytemplate.asp > variable = "category3"Any suggestions gratefully received.Thanks. :)

Link to post
Share on other sites

You can use a combination of dirname and basename:$dir = basename(dirname($file));Hold on, that was for PHP.If you're using VBScript you'll probably have to get the position of the last slash, remove everything after it (including the slash), then get the position of the new last slash, and return everything after that. So you would strip off the filename and slash, then find the position of the last slash and get everything after it. You can also use split to break the string up in an array, and then get the second to last element in the array:http://www.w3schools.com/VBscript/func_split.aspJavascript would be similar, Javascript has a split method for strings that will split the string up the same way:

var file = 'myurl/category1/mytemplate.asp';var chunks = file.split('/');var dir = chunks[chunks.length-2];

Link to post
Share on other sites
  • 2 weeks later...

Found an easy way to do what I wanted<%Dim fs,fo,catSet fs=Server.CreateObject("Scripting.FileSystemObject")Set fo=fs.GetFolder(server.MapPath("."))cat=fo.nameset fo=nothingset fs=nothing%>Tested and seems to work. <%=cat%> produces the current folder name.Does anyone think the above code could create unknown problems?Thanks

Edited by narrator
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...