CFLib.org – Common Function Library Project

GetPathFromURL(this_url)

Last updated February 22, 2002

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Returns the path (minus the file name) for the passed URL. If no path is found, then returns an empty string. Works with any protocol that follows the standard "/path/" syntax include http, ftp, and others. Relative and absolute URLs are accepted. The last "/" character can be implied, but the final component name must not have any dots (".") or it will be considered a file name (see GetFileFromURL).

Return Values:
Returns a string.

Example:

<cfoutput>
#GetPathFromURL("ftp://user:password@somehost.com/dir1/dir2/filename.htm")#<br>
#GetPathFromURL("ftp://user:password@somehost.com/dir1/dir2")#<br>
#GetPathFromURL("ftp://user:password@somehost.com/")#<br>
#GetPathFromURL("ftp://user:password@somehost.com")#<br>
#GetPathFromURL("somehost.com:80/dir1/dir2/filename.htm")#<br>
#GetPathFromURL("somehost.com/dir1/dir2/filename.htm")#<br>
#GetPathFromURL("/dir1/dir2/filename.htm")#<br>
#GetPathFromURL("../../dir1/dir2/filename.htm")#<br>
</cfoutput>

Parameters:

Name Description Required
this_url URL to parse. Yes

Full UDF Source:

/**
 * Returns the path from a specified URL.
 * 
 * @param this_url      URL to parse. 
 * @return Returns a string. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, February 21, 2002 
 */
function GetPathFromURL(this_url) {
    var first_char        = "";
    var re_found_struct   = "";
    var path              = "";
    var i                 = 0;
    var cnt               = 0;
    
    this_url = trim(this_url);
    
    first_char = Left(this_url, 1);
    if (Find(first_char, "./")) {
        // relative URL (ex: "../dir1/filename.html" or "/dir1/filename.html")
        re_found_struct = REFind("[^##\?]+", this_url, 1, "True");
    } else if(Find("://", this_url)){
        // absolute URL    (ex: "ftp://user:pass@ftp.host.com")
        re_found_struct = REFind("/[^##\?]*", this_url, Find("://", this_url)+3, "True");
    } else {
        // abbreviated URL (ex: "user:pass@ftp.host.com")
        re_found_struct = REFind("/[^##\?]*", this_url, 1, "True");
    }
    
    if (re_found_struct.pos[1] GT 0)  {
        // get path with filename (if exists)
        path = Mid(this_url, re_found_struct.pos[1], re_found_struct.len[1]);
        
        // chop off the filename
         if(find("/", path)) {
            i = len(path) - find("/" ,reverse(path)) +1;
            cnt = len(path) -i;
            if (cnt LT 1) cnt =1;
            if (REFind("[^##\?]+\.[^##\?]+", Right(path, cnt))){
                // if the part after the last slash is a file name then chop it off
                path = Left(path, i);
            }
        }
        return path;
    } else {
        return "";
    }
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson