get filename from path without _splitpath

18 篇文章 0 订阅

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值