CFLib.org – Common Function Library Project

getRelative(abspath)

Last updated August 30, 2012

author

Isaac Dealey

Version: 2 | Requires: CF5 | Library: FileSysLib

Description:
Use getRelative() is the inverse of the ColdFusion native ExpandPath() function -- it takes an absolute file path and returns a relative path to the given file from the current template. As opposed to expandpath(), getRelative() creates a path relative to the current template, not the base template.

Return Values:
Returns a string.

Example:

<cfoutput>#getRelative('d:\temp\test.txt')#</cfoutput>

Parameters:

Name Description Required
abspath Absolute path. Yes

Full UDF Source:

/**
 * Returns a relative path from the current template to an absolute file path.
 * v2 fix by Tony Monast
 * v2.1 fix by Tony Monast to deal with situations in which the specified path was the same as the current path, resulting in an error
 * 
 * @param abspath      Absolute path. (Required)
 * @return Returns a string. 
 * @author Isaac Dealey (info@turnkey.to) 
 * @version 2, August 30, 2012 
 */
function getRelative(abspath) {
        var currentPath = ListToArray(GetDirectoryFromPath(GetBaseTemplatePath()),"\/");
        var filePath = ListToArray(abspath,"\/");
        var relativePath = ArrayNew(1);
        var pathStart = 0;
        var i = 0;

        /* Define the starting path (path in common) */
        for (i = 1; i LTE ArrayLen(currentPath); i = i + 1) {

                if (currentPath[i] NEQ filePath[i]) {
                        pathStart = i;
                        break;
                }
        }

        if (pathStart GT 0) {
                /* Build the prefix for the relative path (../../etc.) */
                for (i = ArrayLen(currentPath) - pathStart ; i GTE 0 ; i = i - 1) {
                        ArrayAppend(relativePath,"..");
                }

                /* Build the relative path */
                for (i = pathStart; i LTE ArrayLen(filePath) ; i = i + 1) {
                        ArrayAppend(relativePath,filePath[i]);
                }
        }

        /* Same level */
        else
                ArrayAppend(relativePath,filePath[ArrayLen(filePath)]);

        /* Return the relative path */
        return ArrayToList(relativePath,"/");
}

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