Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

SplitPath

2.84/5 (16 votes)
7 Sep 2005 1   1.2K  
Splitting a path into its components.

Sorry, it should have been SplitPath.jpg

Introduction

Most applications have to handle path names somewhere along the way. This simple class (actually it's more a collection of static methods) provides nine functions that make the different parts of a path accessible. Groundbreaking ideas are not included, but creating relative path names in any direction across a drive might be a useful feature.

Using the code

The most often problems should have the most easiest solutions. So here it comes:

// get drive letter
CString drive = PathInfo::getDrive(Path);
// get directory
CString dir = PathInfo::getDirectory(Path);
// get folder
CString folder = PathInfo::getFolder(Path);
// get name (title + extension)
CString name = PathInfo::getName(Path);
// get title
CString title = PathInfo::getTitle(Path);
// get extension
CString extsn = PathInfo::getExtension(Path);
// change extension ("cfg" for example)
CString cfgPath = PathInfo::getPathAs(Path, "cfg");
// switch absolute to relative path
CString relPath = PathInfo::getRelPath(Path, Origin);
// switch relative to absolute path
CString absPath = PathInfo::getAbsPath(relPath, Origin);

History

Version 1.1

Changed:

  • The GUI now allows textual input for testing purpose.
  • getRelPath() now uses _strnicmp() to compare strings.

Version 1.0

Added:

  • getDrive()
  • getDirectory()
  • getFolder()
  • getName()
  • getTitle()
  • getExtension()
  • getPathAs()
  • getRelPath()
  • getAbsPath()

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here