Click here to Skip to main content
15,900,378 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 82.2K   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 
Paste this URL into VS2003:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2005JAN.1033/shellcc/platform/shell/reference/shlwapi/path/path.htm

Shell Path Handling Functions

--------------------------------------------------------------------------------
This section describes the Microsoft Windows Shell path handling functions. The programming elements explained in this documentation are exported by Shlwapi.dll and defined in Shlwapi.h and Shlwapi.lib.


PathAddBackslash
Adds a backslash to the end of a string to create the correct syntax for a path. If the source path already has a trailing backslash, no backslash will be added.

PathAddExtension
Adds a file extension to a path string.

PathAppend
Appends one path to the end of another.

PathBuildRoot
Creates a root path from a given drive number.

PathCanonicalize
Canonicalizes a path.

PathCombine
Concatenates two strings that represent properly formed paths into one path, as well as any relative path pieces.

PathCommonPrefix
Compares two paths to determine if they share a common prefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".

PathCompactPath
Truncates a file path to fit within a given pixel width by replacing path components with ellipses.

PathCompactPathEx
Truncates a path to fit within a certain number of characters by replacing path components with ellipses.

PathCreateFromUrl
Takes a file URL and converts it to a Microsoft MS-DOS path.

PathFileExists
Determines whether a path to a file system object such as a file or directory is valid.

PathFindExtension
Searches a path for an extension.

PathFindFileName
Searches a path for a file name.

PathFindNextComponent
Parses a path for the next path component.

PathFindOnPath
Searches for a file.

PathFindSuffixArray
Determines if a given file name has one of a list of suffixes.

PathGetArgs
Finds the command line arguments within a given path.

PathGetCharType
Determines the type of character with respect to a path.

PathGetDriveNumber
Searches a path for a drive letter within the range of 'A' to 'Z' and returns the corresponding drive number.

PathIsContentType
Determines if a file's registered content type matches the specified content type. This function obtains the content type for the specified file type and compares that string with the pszContentType. The comparison is not case sensitive.

PathIsDirectory
Verifies that a path is a valid directory.

PathIsDirectoryEmpty
Determines whether or not a specified path is an empty directory.

PathIsFileSpec
Searches a path for any path delimiting characters (for example, ':' or '\' ). If there are no path delimiting characters present, the path is considered to be a File Spec path.

PathIsHTMLFile
Determines if a file is an HTML file. The determination is made based on the content type that is registered for the file's extension.

PathIsLFNFileSpec
Determines whether or not a file name is in long format.

PathIsNetworkPath
Determines whether a path string represents a network resource.

PathIsPrefix
Searches a path to determine if it contains a valid prefix of the type passed by pszPrefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".

PathIsRelative
Searches a path and determines if it is relative.

PathIsRoot
Parses a path to determine if it is a directory root.

PathIsSameRoot
Compares two paths to determine if they have a common root component.

PathIsSystemFolder
Determines if an existing folder contains the attributes that make it a system folder. Alternately indicates if certain attributes qualify a folder to be a system folder.

PathIsUNC
Determines if the string is a valid Universal Naming Convention (UNC) for a server and share path.

PathIsUNCServer
Determines if a string is a valid UNC for a server path only.

PathIsUNCServerShare
Determines if a string is a valid UNC share path, \\ server\ share.

PathIsURL
Tests a given string to determine if it conforms to a valid URL format.

PathMakePretty
Converts a path to all lowercase characters to give the path a consistent appearance.

PathMakeSystemFolder
Gives an existing folder the proper attributes to become a system folder.

PathMatchSpec
Searches a string using a MS-DOS wild card match type.

PathParseIconLocation
Parses a file location string containing a file location and icon index, and returns separate values.

PathQuoteSpaces
Searches a path for spaces. If spaces are found, the entire path is enclosed in quotation marks.

PathRelativePathTo
Creates a relative path from one file or folder to another.

PathRemoveArgs
Removes any arguments from a given path.

PathRemoveBackslash
Removes the trailing backslash from a given path.

PathRemoveBlanks
Removes all leading and trailing spaces from a string.

PathRemoveExtension
Removes the file extension from a path, if there is one.

PathRemoveFileSpec
Removes the trailing file name and backslash from a path, if it has them.

PathRenameExtension
Replaces the extension of a file name with a new extension. If the file name does not contain an extension, the extension will be attached to the end of the string.

PathSearchAndQualify
Determines if a given path is correctly formatted and fully qualified.

PathSetDlgItemPath
Sets the text of a child control in a window or dialog box, using PathCompactPath to make sure the path fits in the control.

PathSkipRoot
Parses a path, ignoring the drive letter or UNC server/share path parts.

PathStripPath
Removes the path portion of a fully qualified path and file.

PathStripToRoot
Removes all parts of the path except for the root information.

PathUndecorate
Removes the decoration from a path string.

PathUnExpandEnvStrings
Takes a fully qualified path, and replaces several folder names with their associated environment string.

PathUnmakeSystemFolder
Removes the attributes from a folder that make it a system folder. This folder must actually exist in the file system.

PathUnquoteSpaces
Removes quotes from the beginning and end of a path.

SHSkipJunction
Checks a bind context to see if it is safe to bind to a particular component object.

UrlApplyScheme
Takes a URL string, determines a scheme for it, and returns a string with an appropriate prefix.

UrlCanonicalize
Takes a URL string and converts it into canonical form.

UrlCombine
When provided with a relative URL and its base, returns a URL in canonical form.

UrlCompare
Makes a case-sensitive comparison of two URL strings.

UrlCreateFromPath
Takes a MS-DOS path and converts it to a canonicalized URL.

UrlEscape
Converts characters in a URL that might be altered during transport across the Internet ("unsafe" characters) into their corresponding escape sequences.

UrlEscapeSpaces
A macro that converts space characters into their corresponding escape sequence.

UrlGetLocation
Retrieves the location from a URL.

UrlGetPart
Accepts a URL string and returns a specified part of that URL.

UrlHash
Hashes a URL string.

UrlIs
Tests whether or not a URL is a specified type.

UrlIsFileUrl
Tests a URL to determine if it is a file URL.

UrlIsNoHistory
Returns whether or not a URL is a No History URL.

UrlIsOpaque
Returns whether a URL is opaque.

UrlUnescape
Converts escape sequences back into ordinary characters.

UrlUnescapeInPlace
Converts escape sequences back into ordinary characters and overwrites the original string.




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 
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.