Recent Changes (August 2005)
CPath is a string class customized to handle path and file names on Windows.
Working with file names is often a pain:
- Correct concatenation using
file = path & subfolder & name;
- Splitting into components (path, file name, title, extension, parent folder, root, and individual elements).
- Cleanup, like trimming, un- and requoting.
- Read from / Write to Registry with one command.
- Expanding / Collapsing Environment strings.
- Detecting type of the path (
IsURL, etc.) and a variety of root types (drive, long path, server, share, protocol, pseudo-protocol).
- Basic file system operations:
- Frequent Helpers like
The project includes a test application where you can play around, explore and test the functionality.
Design and Dependencies:
The main design goal was comfortable use. It is based on
CString and uses the Shell Lightweight Path Helper API, so it is not suitable for portable applications.
Things you must know
Construction and Assignment (from any
CString) will do some path cleanup. By default, this is: removing quotes, trimming spaces (inside and outside the spaces), collapsing self- and up-references ("
.." and "
."), replacing UNC long path specification ("
\\?\C:\") by the actual root, and expanding environment strings. See
EPathCleanup for other options.
CPath provides an
operator LPCTSTR (similar to
CPath::GetStr() retrieves the path, and allows to apply "repacking" options. By default, paths exceeding
MAX_PATH receive their UNC prefix. See
EPathPacking for more options. Use
CPath::GetStr(0) to retrieve the unmodified contained path as
CString (avoids copy).
CPath functions usually fall back to a "sensible default handling". For methods accessing the file system, call
GetLastError() to retrieve error information.
Things you might want to know
» Concatenate using '&':
CPath path = CPath("C:\\temp") & "foo.txt";.
» Split the path into components:
String root = path.GetRoot();
CPath folder = path.GetPath();
CString fileName = path.GetName();
CString fileTitle = path.GetTitle();
CString ext = path.GetExtension();
» Chain Commands:
» Get Application Directory:
Complete documentation (generated by Doxygen) is available in the download (HTML / HTMLHelp).
Do you want to create similar documentation for your code? My Doxygen article tells you how.
I didn't use
CPathT, because the class is intended for a VC6 project (which won't be ported to VC7 very soon), and
CPathT contains the same misfeatures as the Shell helper functions.
I did use
CString, because it's well available outside of MFC projects (using WTL, or the "extract
CString implementation" macro for DevStudio, or a
CString clone class). Further, the guaranteed reference counting implementation allows for a convenient API without too much performance impact.
CPath acts as an
LPCTSTR if passed as an "unknown" argument to a function with a variable argument list. Remember, however, that this is not portable to other compilers.
Please Note: Breaking changes were introduced with the March 2005 update, when merging two slightly distinct branches. Unfortunately, I opted for keeping my code base intact, not thinking of the article published. (I hope you don't mind too much and you still like the update.) All changes will cause a compiler error.
- June 20, 2004: Initial release.
- June 22, 2004
nsPath::CPath::MakeSystemFolder implements unmake correctly.
nsPath::CPath::SearchOnPath set the Windows error code to zero if the function succeeds (thanks Hans Dietrich).
nsPath::CPath compiles correctly with warning level -W4.
- Mar 3, 2005
- Doxygen class member documentation now uses groups for better orientation.
eppAutoQuote bug in
GetStr (thanks Stlan).
GetRoot has a new implementation.
- Breaking changes (sorry)
ShellGetRoot (to distinct from the new, extended
- Made the creation functions independent functions in the
nsPath namespace (they are well tugged away in the namespace so conflicts are avoided anyway).
- Mar 17, 2005
- Fixed bug in
GetName): if the path ends in a backslash,
GetFileName did return the entire path instead of an empty string (thanks woodland).
- Aug 25, 2005