From: http://www.eggheadcafe.com/software/aspnet/29583177/get-folder-from-full-file-path.aspx
//
// Returns the file portion from a path
//
CString GetFileOnly(LPCTSTR Path)
{
// Strip off the path and return just the filename part
CString temp = (LPCTSTR) Path; // Force CString to make a copy
::PathStripPath(temp.GetBuffer(0));
temp.ReleaseBuffer(-1);
return temp;
}
//
// Returns the folder portion from a path
//
CString GetFolderOnly(LPCTSTR Path)
{
// Strip off the file name so we can direct the file scanning dialog to
go
// back to the same directory as before.
CString temp = (LPCTSTR) Path; // Force CString to make a copy
::PathRemoveFileSpec(temp.GetBuffer(0));
temp.ReleaseBuffer(-1);
return temp;
}
//
// Adds a backslash to the end of a path if it is needed
//
CString AddSlash(LPCTSTR Path)
{
CString cs = Path;
::PathAddBackslash(cs.GetBuffer(_MAX_PATH));
cs.ReleaseBuffer(-1);
if(cs.IsEmpty())
cs = _T("\\");
return cs;
}
//
// Removes a backslash from the end of a path if it is there
//
CString RemoveSlash(LPCTSTR Path)
{
CString cs = Path;
::PathRemoveBackslash(cs.GetBuffer(_MAX_PATH));
cs.ReleaseBuffer(-1);
return cs;
}
//
// Adds a folder path and file together to make a file path
//
CString AddPathAndFile(LPCTSTR Folder, LPCTSTR File)
{
CString cs = Folder;
::PathAddBackslash(cs.GetBuffer(_MAX_PATH));
::PathAppend(cs.GetBuffer(_MAX_PATH),File);
cs.ReleaseBuffer(-1);
return cs;
}
//
// Adds a .ext to the end of a file path
//
CString AddExt(LPCTSTR Path, LPCTSTR Ext)
{
CString cs = Path;
::PathAddExtension(cs.GetBuffer(_MAX_PATH),Ext);
cs.ReleaseBuffer(-1);
return cs;
}
//
// Returns true if file exists or false if file does not exist
//
BOOL FileExists(LPCTSTR Path)
{
return (::PathFileExists(Path));
}
//
// Returns just the .ext part of the file path
//
CString GetFileExt(LPCTSTR Path)
{
CString cs;
cs = ::PathFindExtension(Path);
return cs;
}
CString GetFileName(LPCTSTR Path)
{
CString csFile = GetFileOnly(Path);
if(!GetFileExt(csFile).IsEmpty()) { // Is there an extension
::PathRemoveExtension(csFile.GetBuffer(_MAX_PATH));
csFile.ReleaseBuffer();
}
return csFile;
}
//
// Exchanges one file extension for another and returns the new fiel path
//
CString RenameFileExt(LPCTSTR Path, LPCTSTR Ext)
{
CString cs = Path;
::PathRenameExtension(cs.GetBuffer(_MAX_PATH), Ext);
return cs;
}