Extract the file name from the full file path.

Reply

Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Extract the file name from the full file path.

 
0
  #1
Nov 8th, 2007
Hi all,

Using my application I've found a file path anywhere on my machine and store the full path in a CString variable. That is full path, like this,

  1. G:\Work On\CPP\001_002_003.txt

What I want to do is, find the name of the file(without the extension), 001_002_003 according to my example. Then separate as 001 002 and 003, then write them to a file in three columns.

I'm stuck with how to get that three part, 001 002 and 003, from the full file path.

Thanks.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Extract the file name from the full file path.

 
0
  #2
Nov 8th, 2007
DOS used to have a 'splitpath' function which returned the various components of a full path.

Or you could do
- find the last \ to mark the start of the filename
- find the last . to mark the end of the filename.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Extract the file name from the full file path.

 
0
  #3
Nov 8th, 2007
Originally Posted by Salem View Post
Or you could do
- find the last \ to mark the start of the filename
- find the last . to mark the end of the filename.
Can you tell me about this more. Any worked example really appreciate.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Extract the file name from the full file path.

 
0
  #4
Nov 8th, 2007
Actually, you should probably Read This!, then post back.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Extract the file name from the full file path.

 
0
  #5
Nov 8th, 2007
Great.......
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Extract the file name from the full file path.

 
0
  #6
Nov 8th, 2007
> Can you tell me about this more. Any worked example really appreciate.
What did your last slave die of?

Are you totally incapable of digging around in the manual pages to see what the CString API is capable of, or do you have to be told every single detail in advance before you try to use it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Extract the file name from the full file path.

 
0
  #7
Nov 8th, 2007
Ok, at the end I have done it myself using MSDN. Used CFindFile class and FindNextFile() to do that. Like this.

  1. CFileFind fileName;
  2.  
  3. static const TCHAR UseFile[] = _T("F:\\Resources\\Files\\ReadFile.txt");
  4.  
  5. BOOL ress = fileName.FindFile(UseFile);
  6. fileName.FindNextFile();
  7. if(ress)
  8. {
  9. AfxMessageBox((LPCSTR)fileName.GetFileTitle(), MB_OK);
  10. }

I want to use a CString here, because file path is on CString format, which I have already found. Can you give me a clue how to use it here in this code.

That mean I have to cast CString to a char buffer, is that right? Is it possible?
Last edited by eranga262154; Nov 8th, 2007 at 7:46 am. Reason: Adding more
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Extract the file name from the full file path.

 
0
  #8
Nov 8th, 2007
OMG why in the world are you doing that just to extract the name part of the string! If the CString already contains the full path + filename just use its Find() method to locate the last '\' and you have the name starting with the next character.

>>That mean I have to cast CString to a char buffer, is that right? Is it possible?
Depends -- is your program compiled for UNICODE or not. If it is, then CString can not be simply typecase, it must be converted from wchar_t* to char* with one of the conversion functions.
Last edited by Ancient Dragon; Nov 8th, 2007 at 8:38 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Extract the file name from the full file path.

 
0
  #9
Nov 8th, 2007
I have tried one thing. Use ReverseFind() method of CString class to find the last '\' sign. Then I used the Right() to get the required file name. I can use Right() easily because file name length is always fixed.
  1. CString ss = "F:\\Files\\ReadFile.txt";
  2. CString aa;
  3.  
  4. if(ss.ReverseFind('\\')== 8)
  5. {
  6. AfxMessageBox(ss.Right(12), MB_OK);
  7. aa = ss.Right(12);
  8. }
  9.  
  10. if(aa.ReverseFind('.')==8)
  11. {
  12. AfxMessageBox(aa.Left(8), MB_OK);
  13. }

Actually this code gives the file name, ReadFile.

But there is a real issue, on the first 'if' condition. First part of the path, that is "F:\\Files" is not fixed. So in the first 'if' condition number 8 can be change depend on the file selection on my PC. How can avoid that.
Last edited by eranga262154; Nov 8th, 2007 at 11:36 pm. Reason: Missing code tags
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Extract the file name from the full file path.

 
0
  #10
Nov 9th, 2007
> if(ss.ReverseFind('\\')== 8) What does this return if there is no \\ ?

In other words, do something like
  1. int pathStart = ss.ReverseFind('\\');
  2. if ( pathStart >= 0 ) {
  3. // using the position and length of the string, you can now do say
  4. aa = ss.Right(12);
  5. // but replace the 12 with some kind of calculation based on the dynamic
  6. // results you've calculated so far
  7. }

Also, don't assume that the extension is
a) always present
b) always 3 characters.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC