Function to Determine a File's Relative Path


Application Security Testing: An Integral Part of DevOps

As far as I can see, there is no API function to do what I want so I have written one.

What GetRelativeFilename does is to take a base directory and an absolute filename and return a relative filename. For example:

Base Directory: c:\foo\bar
Filename: c:\foo\bar\myfile.txt
Relative name: myfile.txt

Base Directory: c:\foo\bar
Filename: c:\foo\myfile.txt
Relative name: ..\myfile.txt

Base Directory: c:\foo\bar
Filename: c:\goo\gia\myfile.txt
Relative name: ..\..\goo\gai\myfile.txt

You can use the various #defines to customise the function. MAX_FILENAME_LEN speaks for itself, ABSOLUTE_NAME_START controls the characters which are ignored at the start of an absolute filename ("c:\" for dos = 3 characters, "\" for unix = 1 character). SLASH is the character that separates directory names, i.e. '\\' for dos.

The function prototype is:

char* GetRelativeFilename(char *currentDirectory, char *absoluteFilename);
Both currentDirectory and absoluteFilename should be an absolute name, i.e. start with "c:\" for dos names or "/" for unix names. currentDirectory can end with a slash or not, it makes no difference.

To cope with dos drive letters, if the first character of currentDirectory is not equal to the first character of absoluteFilename, then an exact copy of absoluteFilename is returned. This should not be a problem with unix filenames as all absolute names start with "/". For example:

Base Directory: c:\foo\bar
Filename: d:\goo\gai\myfile.txt
Relative name: d:\goo\gai\myfile.txt

Here is the source code:

// GetRelativeFilename(), by Rob Fisher.

// includes

// defines
#define MAX_FILENAME_LEN 512

// The number of characters at the start of an absolute filename.  e.g. in DOS,
// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
// with "\" so this value should be 1.

// set this to '\\' for DOS or '/' for UNIX
#define SLASH '\\'

// Given the absolute current directory and an absolute file name, returns a relative file name.
// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
// GetRelativeFilename will return ..\whee\text.txt.

char* GetRelativeFilename(char *currentDirectory, char *absoluteFilename)
	// declarations - put here so this should work in a C compiler
	int afMarker = 0, rfMarker = 0;
	int cdLen = 0, afLen = 0;
	int i = 0;
	int levels = 0;
	static char relativeFilename[MAX_FILENAME_LEN+1];

	cdLen = strlen(currentDirectory);
	afLen = strlen(absoluteFilename);
	// make sure the names are not too long or too short
		return NULL;
	// Handle DOS names that are on different drives:
	if(currentDirectory[0] != absoluteFilename[0])
		// not on the same drive, so only absolute filename will do
		strcpy(relativeFilename, absoluteFilename);
		return relativeFilename;

	// they are on the same drive, find out how much of the current directory
	// is in the absolute filename
	while(i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i])

	if(i == cdLen && (absoluteFilename[i] == SLASH || absoluteFilename[i-1] == SLASH))
		// the whole current directory name is in the file name,
		// so we just trim off the current directory name to get the
		// current file name.
		if(absoluteFilename[i] == SLASH)
			// a directory name might have a trailing slash but a relative
			// file name should not have a leading one...

		strcpy(relativeFilename, &absoluteFilename[i]);
		return relativeFilename;

	// The file is not in a child directory of the current directory, so we
	// need to step back the appropriate number of parent directories by
	// using "..\"s.  First find out how many levels deeper we are than the
	// common directory
	afMarker = i;
	levels = 1;

	// count the number of directory levels we have to go up to get to the
	// common directory
	while(i < cdLen)
		if(currentDirectory[i] == SLASH)
			// make sure it's not a trailing slash
			if(currentDirectory[i] != '\0')

	// move the absolute filename marker back to the start of the directory name
	// that it has stopped in.
	while(afMarker > 0 && absoluteFilename[afMarker-1] != SLASH)

	// check that the result will not be too long
	if(levels * 3 + afLen - afMarker > MAX_FILENAME_LEN)
		return NULL;
	// add the appropriate number of "..\"s.
	rfMarker = 0;
	for(i = 0; i < levels; i++)
		relativeFilename[rfMarker++] = '.';
		relativeFilename[rfMarker++] = '.';
		relativeFilename[rfMarker++] = SLASH;

	// copy the rest of the filename into the result string
	strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);

	return relativeFilename;


  • Existing API

    Posted by Legacy on 07/30/1999 12:00am

    Originally posted by: Philip Lee

    Try PathRelativePathTo which is part of the shell lightweight utility APIs

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Mobility is reaching an inflection point in enterprise. Smart enterprises are moving away from tactical point solutions for mobile device management, looking instead for a secure, comprehensive, unified, and future-proof mobile-app platform. Enterprise applications are now at the heart of mobile productivity, data is shared outside the enterprise walls, and the data itself may reside on multiple devices and clouds. Read this guide to learn how a unified endpoint management (UEM) solution is critical to …

  • IT planners have far more options as to where to run their workloads than ever before. On-premises data centers, co-location facilities and managed services providers are now joined by hybrid multi-clouds – a combination of Software-, Infrastructure- and Platform-as-a-Service (SaaS, IaaS, and PaaS) execution venues. All have unique operational, performance and economic characteristics that need to be considered when deploying workloads. Submit the form to watch this webinar replay featuring 451 …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date