How to get file name from a full path
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
Since you want to use API (Windows SDK), the function below meets your need. But
CFile
is even convenient for you though.BOOL GetFileInformationByHandleEx(
HANDLE hFile,
FILE_INFO_BY_HANDLE_CLASS cls,
LPVOID info,
DWORD bufsize
);
// ...
FILE_NAME_INFO MyStruct = {0};
bool b = GetFileInformationByHandleEx(hFile, FileNameInfo, &MyStruct, sizeof(FILE_NAME_INFO));
Maxwell Chen
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
PathStripPath()
nave [OpenedFileFinder]
-
Since you want to use API (Windows SDK), the function below meets your need. But
CFile
is even convenient for you though.BOOL GetFileInformationByHandleEx(
HANDLE hFile,
FILE_INFO_BY_HANDLE_CLASS cls,
LPVOID info,
DWORD bufsize
);
// ...
FILE_NAME_INFO MyStruct = {0};
bool b = GetFileInformationByHandleEx(hFile, FileNameInfo, &MyStruct, sizeof(FILE_NAME_INFO));
Maxwell Chen
OK, Thank you Maxwell Chen :) Both of the ways you suggested are convenient. But the question is all of them need a file HANDLE. I just want to get the filename given its fullpath.
A Chinese VC++ programmer
-
PathStripPath()
nave [OpenedFileFinder]
Thank you nave! I think I have got the answer
:_splitpath
:)A Chinese VC++ programmer
-
Thank you nave! I think I have got the answer
:_splitpath
:)A Chinese VC++ programmer
You can use the CFileFind CFileFind cf; cf.FindFile("C:\\Program Files\\codeproject.txt"); cf.FindNext(); CString strFileName = cf.GetFileName(); Rgds Abhay..
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.
Come online at:- jubinc@skype
-
Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.
Come online at:- jubinc@skype
Don Box wrote:
still I'll strongly recommend u to try writing such function.
Less u write, less will be the bugs...
nave [OpenedFileFinder]
-
Don Box wrote:
still I'll strongly recommend u to try writing such function.
Less u write, less will be the bugs...
nave [OpenedFileFinder]
-
Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.
Come online at:- jubinc@skype
I do have written a function to do make the job done, but I still want to know is there some exist functions can do this job for me. :)
A Chinese VC++ programmer
-
If basics are not clear and don't have confidence, then better don't write (such simple function) and drop the programming profession.
Come online at:- jubinc@skype
cool man..I just said one truth regrding the programming field. How ever I still remeber one bug while trying to extract the file name from path. We were extracting the file name by reverse finding the "\". But later a bug was reported and reason was that, in one secnario in the path input to that function, instead of "\", seperator was "/" :(
nave [OpenedFileFinder]
-
cool man..I just said one truth regrding the programming field. How ever I still remeber one bug while trying to extract the file name from path. We were extracting the file name by reverse finding the "\". But later a bug was reported and reason was that, in one secnario in the path input to that function, instead of "\", seperator was "/" :(
nave [OpenedFileFinder]
-
I do have written a function to do make the job done, but I still want to know is there some exist functions can do this job for me. :)
A Chinese VC++ programmer
-
Don Box wrote:
still I'll strongly recommend u to try writing such function.
Less u write, less will be the bugs...
nave [OpenedFileFinder]
Naveen wrote:
Less u write, less will be the bugs...
Write no code and you'll have absolutely no bugs. :-\
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
You may use
PathFindFileName()
like this:TCHAR \*szPath = \_T("C:\\\\Program Files\\\\codeproject.txt"); AfxMessageBox(PathFindFileName(szPath));
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Suppose the full path is :
C:\Program Files\codeproject.txt
I want to extract the file namecodeproject.txt
. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)A Chinese VC++ programmer
-
Thank you nave! I think I have got the answer
:_splitpath
:)A Chinese VC++ programmer
zengkun100 wrote:
I think I have got the answer:_splitpath
MSDN Says:
_splitpath
is deprecated because more secure versions are available, see_splitpath_s
, _wsplitpath_s
.Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Naveen wrote:
Less u write, less will be the bugs...
Write no code and you'll have absolutely no bugs. :-\
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Yes. but the damn requirments doesnt allow me to be like that :(
nave [OpenedFileFinder]
-
That's right Naveen. BTW, how many yrs of experience do u have in VC++? Do u have any Skype/Yahoo id?
Come online at:- jubinc@skype
Don Box wrote:
Do u have any Skype/Yahoo id?
Yahoo only... Have send my chat id to your mail box. check
nave [OpenedFileFinder]
-
Don Box wrote:
Do u have any Skype/Yahoo id?
Yahoo only... Have send my chat id to your mail box. check
nave [OpenedFileFinder]