Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / MFC
Article

SplitPath

Rate me:
Please Sign up or sign in to vote.
2.84/5 (16 votes)
7 Sep 2005 82K   1.2K   20   15
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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis is not thread safe Pin
CodeScrubber8-Jun-10 10:00
CodeScrubber8-Jun-10 10:00 
Generalthanks Pin
skydave14-Nov-07 22:48
skydave14-Nov-07 22:48 
GeneralHave a look at boost.filesystem Pin
Ghost-Rider8-Sep-05 1:17
Ghost-Rider8-Sep-05 1:17 
GeneralRe: Have a look at boost.filesystem Pin
Achim Klein8-Sep-05 2:19
Achim Klein8-Sep-05 2:19 
GeneralJust use the PathXXX Win32 Functions Pin
TheGreatAndPowerfulOz29-Aug-05 6:41
TheGreatAndPowerfulOz29-Aug-05 6:41 
GeneralRe: Just use the PathXXX Win32 Functions Pin
Achim Klein29-Aug-05 8:18
Achim Klein29-Aug-05 8:18 
GeneralRe: Just use the PathXXX Win32 Functions Pin
milkplus19-Nov-09 7:52
milkplus19-Nov-09 7:52 
I'm not super keen on shell path (Shlwapi.h)
I think your PathInfo.h is very clean and useful.
However, you might want to consider using the same API as System.IO.Path in .NET framework

ChangeExtension - Changes the extension of a path string.
Combine - Combines two path strings.
GetDirectoryName - Returns the directory information for the specified path string.
GetExtension - Returns the extension of the specified path string.
GetFileName - Returns the file name and extension of the specified path string.
GetFileNameWithoutExtension - Returns the file name of the specified path string without the extension.
GetFullPath - Returns the absolute path for the specified path string.
GetPathRoot - Gets the root directory information of the specified path.
GetTempFileName - Returns a uniquely named zero-byte temporary file on disk and returns the full path to that file.
GetTempPath - Returns the path of the current system's temporary folder.
HasExtension - Determines whether a path includes a file name extension.
IsPathRooted - Gets a value indicating whether the specified path string contains absolute or relative path information.
NewsUpdate Pin
Achim Klein27-Aug-05 8:06
Achim Klein27-Aug-05 8:06 
Generalidea for future versions Pin
mgama19-Aug-05 5:05
mgama19-Aug-05 5:05 
GeneralRe: idea for future versions Pin
Herbert Illfelder22-Aug-05 4:44
Herbert Illfelder22-Aug-05 4:44 
QuestionUNC? Pin
Uwe Keim19-Aug-05 3:03
sitebuilderUwe Keim19-Aug-05 3:03 
GeneralMine does... Pin
Kochise19-Aug-05 3:22
Kochise19-Aug-05 3:22 
GeneralRe: Mine does... Pin
peterchen19-Aug-05 23:17
peterchen19-Aug-05 23:17 
GeneralRe: Mine does... Pin
Kochise20-Aug-05 0:19
Kochise20-Aug-05 0:19 
GeneralInteresting Pin
Geert van Horrik19-Aug-05 2:58
Geert van Horrik19-Aug-05 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.