PDA

View Full Version : Counting elements in one string to remove from another...


sroughley
06-17-2003, 05:58 AM
Hey all.

It's my first post here and I have only been developing in ASP for a month now (previously worked with PHP) so forgive me if I seem ignorant.

The problem is this. I have a string that contains a user specified web directory that exists on a server (lets say "../../sounds").

I am trying to use this directory to build a string using the request.servervariables("PATH_INFO") that will create a full server path to the sounds folder.

Now, I need to be able to identify how many ../'s are at the beginning of the directory string and remove the same amount of directories from the end of the PATH_INFO string before appending the directory string to the end of the PATH-INFO string.

So if PATH_INFO returns:

//servername/s/somesite.com/htdocs/app/admin/

and we have the directory string:

../../sounds

The final string will be

//servername/s/somesite.com/htdocs/sounds/

Any ideas?

Regards.

Steve.

sroughley
06-17-2003, 07:35 AM
FYI, this is what I am trying at the moment. It works up to a point, but the loop at the bottom doesn't remove the directories as intended.

<%
Dim currentPath
currentPath = request.servervariables("PATH_INFO")
Dim uploadDir
uploadDir = "../../uploadedSounds"
Dim fullPath
currentPath = Trim(Mid(currentPath, 1, InStrRev(currentPath, "/"))) 'remove filename from string'
if (Mid(currentPath, Len(currentPath), 1) <> "/") then
currentPath = currentPath & "/"
end if

Dim i
i = InStrRev(uploadDir, "../") 'Count the number of up dir commands (../)'
i = Round(i / 3) + 1
uploadDir = Replace(uploadDir, "../", "") 'Remove the up dir commands'


'all works just fine until here'
Dim c
for c = 1 to i
currentPath = Trim(Mid(currentPath, 1, InStrRev(currentPath, "/")))
next

fullPath = currentPath & uploadDir
%>

Regards.

Steve.

tev
06-17-2003, 02:37 PM
why exactly do you use the round?

sroughley
06-18-2003, 03:11 AM
It is the same as (i = (i + 2)). it is just to round up to the nearest whole number, as there are three characters in the string: '../', but the InStrRev only counts up to the first character of the string. So, when it comes to division by 3, the number will not be perfectly divisible by three and will allways result in 1.33333, or 2.33333, or 3.33333, etc and I like to keep my equations tidy. As less seems to go wrong when I am dealing with whole numbers.

Regards.

Steve.

sroughley
06-18-2003, 07:23 AM
OK. Basically all I now need is to know how to remove a certain number of directories from the end of a string.

I.e. If I have the number '2' stored in var, then I need to turn this:

somesite/somedir/somesubdir

Into this:

somesite/

Thus removing the last '2' directories.

I should point out that the number of directories to remove will be changing all the time.

Regards.

Steve.

tev
06-20-2003, 05:25 PM
I think you can use split

dim param ="somedir\somedir\something"
dim param2

param2 = split(param,"\")
response.write param(0)

the split will put it into an array

Hope this helps you

tev